当主键具有不同的名称时,如何使用TPT继承模型? [英] How can I use TPT inheritance models when primary keys have different names?

查看:171
本文介绍了当主键具有不同的名称时,如何使用TPT继承模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对遗留数据库使用Entity Framework 4.1,我无法生成一组不复用的TPT继承模型,并为普通主键使用不同的名称。



我正在使用数据库表组织,帐户和公司,如下所示:

 组织
OrganizationID (int PK)
OrgName(varchar)

公司
CompanyID(int PK)
CompanyNo(varchar)

帐户
AccountID(int PK)
AccountNo(varchar)

Account.AccountID and 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);

//覆盖复数
modelBuilder.Entity< Company>()。ToTable(Company);
modelBuilder.Entity< Account>()。ToTable(Account);

//为每个
设置主键列modelBuilder.Entity< Company>()。Property(
e => e.OrganizationID).HasColumnName(CompanyID); (
e => e.OrganizationID)
}

public DbSet< Organization>组织{get;组; }
}

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

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

public partial class公司:Organization
{
public string CompanyNo {get;组; }
}

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


属性OrganizationID不是Company类型上的
声明的属性。
验证该属性是否已被
明确地从
中使用Ignore方法或
NotMappedAttribute数据注释从模型中排除。
确保它是一个有效的原始
属性。




  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映射此处。它是一步一步,所以它应该工作,但你需要映射Ids在子实体。但是问题是您首先使用Model-first,生成依赖于EDMX的DdContext,然后删除该EDMX并自行定义映射。您应该决定是否要使用Model-first或Code-first。你只是在EDMX中浪费了你的努力。



如果你想知道如何使用DbContext模型首先检查这篇文章。如果要使用Code-first,请勿创建EDMX和按照这篇文章映射你的继承。


Using Entity Framework 4.1 against a legacy database, I'm unable to generate a working set of TPT inheritance models that aren't pluralized and use different names for a common primary key.

I am using database tables Organization, Account, and Company as illustrated below:

Organization
  OrganizationID (int PK)
  OrgName (varchar)

Company
  CompanyID (int PK)
  CompanyNo (varchar)

Account
  AccountID (int PK)
  AccountNo (varchar)

Account.AccountID and Company.CompanyID have a FK constraint that values in those columns must also be contained in Organization.OrganizationID so neither can exist without a Organization row. If I were designing these tables from scratch, both Account and Company would use OrganizationID as their primary key instead.

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; }
}

When I try to use the simple select code listed below, I receive the error:

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();
}

解决方案

I described TPT mapping here. It is step by step so it should work but you need to map Ids in child entities.

But the issue is that you start with Model-first, generate DdContext which is dependent on EDMX, then delete that EDMX and start defining mapping by yourselves. You should decide if you want to use Model-first or Code-first. You just wasted your effort in EDMX.

If you want to know how to use Model-first with DbContext check this article. If you want to use Code-first don't create EDMX and follow this article to map your inheritance.

这篇关于当主键具有不同的名称时,如何使用TPT继承模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆