【发布时间】:2020-11-19 06:11:25
【问题描述】:
我有使用 Microsoft.EntityFrameworkCore.Sqlite 5.0.0 的 .NET 5.0.0 控制台应用程序。我已经安装了 Microsoft.EntityFrameworkCore.Sqlite 5.0.0、Microsoft.AspNetCore.Identity.EntityFrameworkCore 5.0.0(对于 IdentityUser)和 Microsoft.EntityFrameworkCore.Design 5.0.0(对于 dotnet-ef 迁移 命令)nuget 包。
我的模型代表一组测试,其中用户选择其他用户,在其他一些测试中用户做同样的事情并留下一些评论(为了测试目的,模型被大大简化了)。所以我的 TestAnswer 类使用三级继承:
public class TestAnswerBase
{
public Int64 Id { get; set; }
[Required]
public Int64 UserId { get; set; }
public virtual User User { get; set; }
}
public partial class TestAnswerChild : TestAnswerBase
{
[Required]
public Int64 ChosenId { get; set; }
public virtual User Chosen { get; set; }
}
//If I use this TestAnswerGrandChild variant - it shows an error while creating a migration:
//"Unable to determine the relationship represented by navigation 'TestAnswerChild.Chosen'
//of type 'User'. Either manually configure the relationship, or ignore this property using
//the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."
public partial class TestAnswerGrandChild : TestAnswerChild
{
public string Comment { get; set; }
}
我的 User 类(它包含用户选择某人的测试集合和用户被某人选择的集合):
public partial class User : IdentityUser<Int64>
{
public User()
{
TestAnswerChildChosen = new HashSet<TestAnswerChild>();
TestAnswerChildUser = new HashSet<TestAnswerChild>();
TestAnswerGrandChildChosen = new HashSet<TestAnswerGrandChild>();
TestAnswerGrandChildUser = new HashSet<TestAnswerGrandChild>();
}
[Required]
public string Name { get; set; }
[InverseProperty(nameof(TestAnswerChild.Chosen))]
public virtual ICollection<TestAnswerChild> TestAnswerChildChosen { get; set; }
[InverseProperty(nameof(TestAnswerChild.User))]
public virtual ICollection<TestAnswerChild> TestAnswerChildUser { get; set; }
[InverseProperty(nameof(TestAnswerGrandChild.Chosen))]
public virtual ICollection<TestAnswerGrandChild> TestAnswerGrandChildChosen { get; set; }
[InverseProperty(nameof(TestAnswerGrandChild.User))]
public virtual ICollection<TestAnswerGrandChild> TestAnswerGrandChildUser { get; set; }
}
我的 DbContext 相当简单,但我在这里包含了它的代码:
public partial class MyDbContext : IdentityDbContext<User, IdentityRole<Int64>, Int64>
{
public MyDbContext()
{
}
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public virtual DbSet<TestAnswerChild> TestAnswersChild { get; set; }
public virtual DbSet<TestAnswerGrandChild> TestAnswersGrandChild { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("data source=test.db");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//is necessary because -> "The entity type 'IdentityUserLogin<long>' requires a primary key to be defined"
base.OnModelCreating(modelBuilder);
}
}
所以当我调用 dotnet-ef migrations add InitialCreate 命令示例时,我得到一个错误:
无法确定导航所代表的关系 “用户”类型的“TestAnswerChild.Chosen”。要么手动配置 关系,或使用“[NotMapped]”忽略此属性 属性或在“OnModelCreating”中使用“EntityTypeBuilder.Ignore”。
如果我像这样更改 TestAnswerGrandChild 代码(只有两级继承) - 它会创建一个没有错误的迁移:
//If I use that TestAnswerGrandChild variant - migration is created with no errors
public partial class TestAnswerGrandChild : TestAnswerBase
{
[Required]
public Int64 ChosenId { get; set; }
public virtual User Chosen { get; set; }
public string Comment { get; set; }
}
那么我的问题是什么以及如何解决它(我需要多级继承 - 测试项目大大简化了)?
我已经上传了我的测试项目here(运行update migrations.cmd 得到一个错误)
提前谢谢你。
【问题讨论】:
标签: c# sqlite .net-core entity-framework-core .net-5