entity-framework-4 - 当主键具有不同的名称时,如何使用 TPT 继承模型?

标签 entity-framework-4 code-first entity-framework-4.1 legacy-database

针对遗留数据库使用 Entity Framework 4.1,我无法生成工作的 TPT 继承模型集,这些模型没有复数化并且对公共(public)主键使用不同的名称。

我正在使用数据库表 Organization、Account 和 Company,如下所示:

Organization
  OrganizationID (int PK)
  OrgName (varchar)

Company
  CompanyID (int PK)
  CompanyNo (varchar)

Account
  AccountID (int PK)
  AccountNo (varchar)

Account.AccountID 和 Company.CompanyID 有一个 FK 约束,即这些列中的值也必须包含在 Organization.OrganizationID 中,因此如果没有组织行,它们都不能存在。如果我从头开始设计这些表,Account 和 Company 都会使用 OrganizationID 作为它们的主键。

public partial class BusinessEntities : DbContext
{
    public BusinessEntities()
        : base("name=BusinessEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Organization>().ToTable("Organization");

        // Override pluralization
        modelBuilder.Entity<Company>().ToTable("Company");
        modelBuilder.Entity<Account>().ToTable("Account");

        // Set primary key column for each
        modelBuilder.Entity<Company>().Property(
            e => e.OrganizationID).HasColumnName("CompanyID");
        modelBuilder.Entity<Account>().Property(
            e => e.OrganizationID).HasColumnName("AccountID");
    }

    public DbSet<Organization> Organization { get; set; }
}

public partial class Organization
{
    public int OrganizationID { get; set; }
    public string OrgName { get; set; }
}

public partial class Account : Organization
{
    public string AccountNo { get; set; }
}

public partial class Company : Organization
{
    public string CompanyNo { get; set; }
}

当我尝试使用下面列出的简单选择代码时,我收到错误:

The property 'OrganizationID' is not a declared property on type 'Company'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

static void Main(string[] args)
{
    using (var context = new BusinessEntities())
    {
        foreach (var b in context.Organization.OfType<Company>())
        {
            Console.WriteLine("{0} {1}", b.CompanyNo, b.OrgName);
        }

        foreach (var b in context.Organization.OfType<Account>())
        {
            Console.WriteLine("{0} {1}", b.AccountNo, b.OrgName);
        }
    }
    Console.ReadLine();
}

最佳答案

我描述了 TPT 映射 here .它是一步一步进行的,所以它应该可以工作,但您需要在子实体中映射 ID。

但问题是你先从模型开始,生成依赖于 EDMX 的 DdContext,然后删除那个 EDMX 并开始自己定义映射。您应该决定是要使用模型优先还是代码优先。您只是在 EDMX 上浪费了精力。

如果您想知道如何将模型优先与 DbContext 结合使用,请查看 this article .如果您想使用代码优先,请不要创建 EDMX 和 follow this article映射您的继承。

关于entity-framework-4 - 当主键具有不同的名称时,如何使用 TPT 继承模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5490134/

相关文章:

c# - 不分组的 Entity Framework 总和和平均值

c# - 级联集空 Entity Framework

entity-framework - 如何使用 Entity Framework Code First 为我的数据库提供种子?

entity-framework - Entity Framework 代码优先只读集合

entity-framework - Entity Framework 代码优先 - DbContext 上没有 Detach() 方法

c# - ALTER TABLE DROP COLUMN 失败,因为一个或多个对象访问此列

c# - EF Code First - 如何设置身份种子?

asp.net-mvc-3 - 不在表单上时 MVC3 数据在发布时丢失?

c# - Entity Framework 4 从实体集合中删除对象

c# - 比较两个集合并从一个集合中添加/删除以使它们匹配