EntityFramework 6.3 分层 CodeFirst

本文介绍了如何使用EntityFramework 6.3进行分层CodeFirst开发,包括创建类库、定义实体类、设置上下文、配置数据库连接、生成迁移文件、初始化数据以及添加字段的步骤,详细讲解了每个步骤的操作过程。
摘要由CSDN通过智能技术生成

一:新建一个类库 并 引入EntityFramework 6.3

 

二:新建一个实体类

namespace EFCodeFirst
{
    [Table("System_User")]//数据库中表名
    public class User
    {
        [Key] //主键
        public long UserID { get; set; }
        public string UserName { get; set; }
    }
}

三:建立上下文

    public class EFCodeFirstDbContext:DbContext
    {
        public EFCodeFirstDbContext()
            :base("Default")
        { }

        public DbSet<User> Users { get; set; }
    }

四:配置数据库连接语句

在类库的App.Config文件中添加数据库连接语句

 

五:建立迁移文件

将类库设置为启动项

在程序包管理器控制台,将默认项目修改为EntityFramework数据层

1:在控制台中输入Enable-Migrations 

执行成功后  会在 项目增加一个Migrations的文件夹和Configuration.cs的类

Configuration.cs类

 internal sealed class Configuration : DbMigrationsConfiguration<EFCodeFirst.EFCodeFirstDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(EFCodeFirst.EFCodeFirstDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.
        }
    }

2:在输入Add-Migration AddUser   AddUser为迁移文件名  自己定义

 public partial class AddUser : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.System_User",
                c => new
                    {
                        UserID = c.Long(nullable: false, identity: true),
                        UserName = c.String(),
                    })
                .PrimaryKey(t => t.UserID);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.System_User");
        }
    }

3:在输入Update-Database 

成功后,就可以在数据库中看到表名为System_User的表了

 

 

六:初始化数据

注意:此处在初始时配置,如果表已存在则需要删除已有的表

internal sealed class Configuration : DbMigrationsConfiguration<EFCodeFirst.EFCodeFirstDbContext>
    {
        public Configuration()
        {
            //为true时 为自动迁移   False为手动迁移
            AutomaticMigrationsEnabled = false;
        }
        //数据库初始化
        protected override void Seed(EFCodeFirst.EFCodeFirstDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.

            //初始化一条数据
            User user = new User()
            {
                UserID = 1,
                UserName = "admin"
            };

            context.Users.AddOrUpdate(user);
            context.SaveChanges();
        }
    }

 

在执行命令

Add-Migration AddUser-Init

Update-Database

表System_User就会有一条初始数据

 

七:增加字段

namespace EFCodeFirst
{
    [Table("System_User")]//数据库中表名
    public class User
    {
        [Key] //主键
        public long UserID { get; set; }
        public string UserName { get; set; }
        //我们新加一个字段,并设置默认值 
        public string Password { get; set; } = "123456";
    }
}

 

执行命令

Add-Migration AddUser-Pwd-default

Update-Database 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值