下面是项目结果
EFDemo是API应用
EFDemo.Data是standard库,用来迁移写查询语句
EFDemo.Entity是standard库,用来写实体
EFDemo引用EFDemo.Data
EFDemo.Data引用EFDemo.Entity
EFDemo设为启动项安装nuget包Install-Package Microsoft.EntityFrameworkCore.Tools
EFDemo.Data安装nuget包Install-Package Microsoft.EntityFrameworkCore.SqlServer
编写EFDemo.Entity这里可以使用特性或导航属性来写表之间的关系,但是不推荐,不直观,我这里用API的方式去写表之间的关系
Order类 因为需要User和Product的外键把这个两个属性也写上,后面写关系会用到
public class Order
{
public int OrderId { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
Product 类 Order属性写上,后面写关系会用到,这代表order 和 Product 1对1的关系
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int ProductNum { get; set; }
public Order Order { get; set; }
}
User类 Orders 属性,这代表order 和 User 1对多的关系
public class User
{
public User()
{
Orders = new List<Order>();
}
public int UserId { get; set; }
public string UserName { get; set; }
public List<Order> Orders { get; set; }
}
编写EFDemo.Data 这里写上下文和数据种子,新建MyDbContext类继承DbContext
DbSet是上下文的自动属性,DbContext 会自动为每个属性调用 setter,以设置相应 DbSet 的实例
OnConfiguring重写方法用来写一些数据库配这列如连接字符串,连接字符串我这里没写,在EFDemo里重写了
OnModelCreating重写方法写一些表关系和种子数据,还可以写一些表的操作
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasKey(t => t.UserId);
modelBuilder.Entity<Product>().HasKey(t => t.ProductId);
modelBuilder.Entity<Order>(t =>
{
t.HasKey(x => x.OrderId);
t.HasOne(x => x.Product).WithOne(x => x.Order).HasForeignKey<Order>(x => x.ProductId);
t.HasOne(x => x.User).WithMany(x => x.Orders).HasForeignKey(x => x.UserId);
});
modelBuilder.Entity<User>().HasData(new User[]
{
new User{ UserId = 1, UserName = "张三" },
new User{ UserId = 2, UserName = "李四" },
new User{ UserId = 3, UserName = "麻五" }
});
modelBuilder.Entity<Product>().HasData(new Product[]
{
new Product{ ProductId = 1, ProductName = "烤鸭", ProductNum = 30 },
new Product{ ProductId = 2, ProductName = "火腿", ProductNum = 40 },
new Product{ ProductId = 3, ProductName = "啤酒", ProductNum = 50 }
});
modelBuilder.Entity<Order>().HasData(new Order[]
{
new Order{ OrderId = 1, ProductId = 1, UserId = 2 },
new Order{ OrderId = 2, ProductId = 2, UserId = 2 },
new Order{ OrderId = 3, ProductId = 3, UserId = 2 }
});
}
}
EFDemo的Startup类注入ef的上下文重写OnConfiguring 配置链接字符串
如下json配置文件
Startup注册上下文重写OnConfiguring
services.AddDbContext<MyDbContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("LocaldbContext")));
打开包管理工具的控制台进行数据迁移
选中带有上下文的类库
get-help EntityFrameworkCore 可以看到有哪些迁移命令
常用命令
Add-Migration 生成迁移文件
Remove-Migration 生成迁移文件出错,可以用这个来回滚
Update-Database 更新到数据库
MyDbContextModelSnapshot是生成的迁移文件,20200418120418_1,20200418120418_2是迁移版本