抽象基类与MVC、EF的继承关系
我有这个示例项目:
模型:
public class Country
{
public int ID{get; set;}
public string CountryName{get; set;}
}
public abstract class Subject
{
public int CountryID{get; set;}
}
public class Person : Subject
{
public string PersonName{get; set;}
}
public class Company : Subjet
{
public string CompanyName{get; set;}
}
上下文:
public class SampleContext : DbContext
{
public DbSet<Contry> Countries{get; set;}
public DbSet<Person> Persons{get; set;}
public DbSet<Company> Companies{get; set;}
modelBuilder.Entity<Person>().Map(m => { m.MapInheritedProperties();ToTable("Person"); });
modelBuilder.Entity<Company>().Map(m => { m.MapInheritedProperties();ToTable("Company"); });
}
我收到此错误。
错误3013:从第xxx行开始的映射片段出现问题:缺少表映射:来自表Subject(CountryID)的外键约束Subject_Country
但是,Subject是抽象的,我不会在数据库中使用这样的表。当我在主题类上仅使用 int 或 string 等参数时,一切正常。但是当我想使用某种关系时,我收到此错误。
非常感谢您的帮助。
i have this sample project:
Models:
public class Country
{
public int ID{get; set;}
public string CountryName{get; set;}
}
public abstract class Subject
{
public int CountryID{get; set;}
}
public class Person : Subject
{
public string PersonName{get; set;}
}
public class Company : Subjet
{
public string CompanyName{get; set;}
}
Context:
public class SampleContext : DbContext
{
public DbSet<Contry> Countries{get; set;}
public DbSet<Person> Persons{get; set;}
public DbSet<Company> Companies{get; set;}
modelBuilder.Entity<Person>().Map(m => { m.MapInheritedProperties();ToTable("Person"); });
modelBuilder.Entity<Company>().Map(m => { m.MapInheritedProperties();ToTable("Company"); });
}
And I get this error.
Error 3013: Problem in Mapping Fragment starting at line xxx: Missing table mapping: Foreign key constraint Subject_Country from table Subject(CountryID)
But, Subject is abstract and I dont wont such table in DB. When I use on Subject class only parameters like int or string, all is ok. But when I want use there some relationship, I get this error.
Thanks a lot for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 SampleContext 中,“国家/地区”的名称拼写错误。
应该是:
In your SampleContext the name of the 'Country' is misspelled.
Should be: