【问题标题】:Inheritance and relationship on base abstract class with MVC, EF基于MVC、EF的基抽象类的继承与关系
【发布时间】:2011-11-02 11:30:59
【问题描述】:

我有这个示例项目:

型号:

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

但是,主题是抽象的,我不会在 DB 中使用这样的表。当我在 Subject 类上仅使用 int 或 string 等参数时,一切正常。但是当我想在那里使用一些关系时,我得到了这个错误。

非常感谢您的帮助。

【问题讨论】:

  • 公共 DbSet 国家{get; set;} 应该是 public DbSet Country{get;设置;}

标签: asp.net-mvc entity-framework inheritance relationship


【解决方案1】:

在您的 SampleContext 中,“国家/地区”的名称拼写错误。

public DbSet<Contry> Countries{get; set;} 

应该是:

public DbSet<Country> Countries{get; set;} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2012-05-20
    • 1970-01-01
    • 2013-10-22
    • 2014-08-16
    相关资源
    最近更新 更多