ef core(code first) 通用性迁移步骤

  1. 创建Models类
  2. 创建DbContext派生类
  3. 添加Provider的nuget包,这里以mysql为例:
    1. Pomelo.EntityFrameworkCore.MySql
    2. Pomelo.EntityFrameworkCore.MySql.Design
  4. 在dbcontext的OnConfiguring重写方法中添加

    if (!optionsBuilder.IsConfigured)
    {
    #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
    optionsBuilder.UseMySql(ConnectString);
    }

    1. 在其它地方配置也可以,比如在AspNetCore项目中,通常在Startup类中配置
  5. dotnet ef migrations add {migration name} -p {dbcontext project} -s {host startup project}
  6. dotnet ef database update -p {dbcontext project} -s {host startup project}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的仓储系统数据库设计,使用 .NET EF Core Code First: ```csharp public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int Quantity { get; set; } public int WarehouseId { get; set; } public Warehouse Warehouse { get; set; } } public class Warehouse { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public List<Product> Products { get; set; } } public class Order { public int Id { get; set; } public DateTime OrderDate { get; set; } public int ProductId { get; set; } public Product Product { get; set; } public int Quantity { get; set; } public decimal TotalPrice { get; set; } } public class WarehouseContext : DbContext { public DbSet<Product> Products { get; set; } public DbSet<Warehouse> Warehouses { get; set; } public DbSet<Order> Orders { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=WarehouseDb;Trusted_Connection=True;"); } } ``` 上述代码定义了三个实体类:Product、Warehouse 和 Order,它们分别表示产品、仓库和订单。每个产品都属于一个仓库,一个仓库可以包含多个产品。订单中包含了要购买的产品和数量以及总价。DbContext 包含了三个 DbSet 属性,分别对应这三个实体类的数据库表。在 OnConfiguring 方法中,使用 UseSqlServer 方法指定了数据库连接字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值