型号:GropsAndProducts
,Groups
,Products
:
public class GropsAndProducts
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[ForeignKey("Products")]
public int ProductId { get; set; }
[ForeignKey("Groups")]
public int GroupId { get; set; }
public Groups Groups { get; set; }
public Products Products { get; set; }
}
public class Groups
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Discription { get; set; }
public List<GropsAndProducts> groupAndProducts { get; set; }
}
public class Products
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Unit { get; set; }
[Required]
public int Count { get; set; }
[Required]
public int Price { get; set; }
[Required]
public int Discount { get; set; }
public string Discription { get; set; }
public List<GropsAndProducts> groupAndProducts { get; set; }
}
上下文:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MySite.Models;
using System.Data;
namespace MySite.Data
{
public class DbProductsAndGroupsContext : DbContext
{
public DbProductsAndGroupsContext(DbContextOptions<DbProductsAndGroupsContext> option)
: base (option)
{
}
public DbSet<Products> products;
public DbSet<Groups> groups;
public DbSet<GropsAndProducts> gropsAndProducts;
}
}
启动配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DbProductsAndGroupsContext>(option =>
option.UseMySql(
Configuration.GetConnectionString("DefaultConnection"), x=> x.MigrationsHistoryTable("__MyMigrationsHistory", "mySchema"))
);
services.AddDbContext<DbUserContex>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<Users>()
.AddEntityFrameworkStores<DbUserContex>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
现在的问题是:
第一次迁移(从UserIdentity
开始)已正常进行(即,创建了一个新数据库以及带有记录的表)。但是在我创建了第二个上下文并尝试执行命令(在包管理器中)之后,执行“ EntityFrameworkCore \ Add-Migration -name:asdf -context:DbProductsAndGroupsContext”,然后添加了**空白**迁移。为什么会这样???我将很高兴收到任何信息。
答案 0 :(得分:3)
请我多注意一下您的问题。您的DbSet
需要具有EntityFramework的getter和setter方法,以便能够找到模型,以便可以生成所需的迁移。
您应该更改:
public DbSet<Products> products;
public DbSet<Groups> groups;
public DbSet<GropsAndProducts> gropsAndProducts;
收件人:
public DbSet<Products> products { get; set; }
public DbSet<Groups> groups { get; set; }
public DbSet<GropsAndProducts> gropsAndProducts { get; set; }
请注意,按照惯例,DbSet
属性名称通常是大写的(即:Products
而不是products
)。要指出的另一件事是,您可以将GropsAndProducts
模型更改为:
public class GropsAndProducts
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public int ProductId { get; set; }
public int GroupId { get; set; }
public Groups Group { get; set; }
public Products Product { get; set; }
}
如果您遵循EF命名约定(Group
导航属性的外键为GroupId
),则不需要外键注释。无论如何,属性名称Groups
可能意义不大,因为该属性仅包含一个Group