如何通过代码优先方法实现实体框架4.3中的继承表类型? [英] How to implement inheritance Table-Per-Type in Entity Framework 4.3 via Code First approach?

查看:105
本文介绍了如何通过代码优先方法实现实体框架4.3中的继承表类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [Table(A)] 
public class A :IIdentification
{
public int id {get;组;
}

[表(B)]
公共类B:A
{
//某些属性
public援助{get; set;}
ForeignKey(Aid);
public A A {get;组; }
}

[Table(C)]
public class C:B
{
// some properties here
public投标{get;组; }
ForeignKey(Bid);
public B B {get;组; }
}

[Table(D)]
public class D:C
{
public Cid {get; set;}
ForeignKey(Cid);
public C C {get;组; }
}

如何为每个类别或任何类别的外键创建一个表其他可能的正确方法?
也许有人可以发布一些样本如何实现?



也许这是一个微不足道的问题,但我花了一整天时间,实现了许多解决方案描述在主题和它还是不行。所以我会非常感谢有人可以帮助我。

解决方案

在映射继承中,您没有可访问的导航属性到父实体,除非你想建立另一个关系。继承是是一个关系,所以B是A而FK是有一个关系。



这里你有一个精彩演练为TPT继承。


I have this class hierarchy in code:

[Table("A")]
public class A : IIdentification
{
    public int id { get; set; }
}

[Table("B")]
public class B : A
{
    //some properties here
    public Aid { get; set;}
    ForeignKey("Aid");
    public A A { get; set; }
}

[Table("C")]
public class C : B
{
    //some properties here 
    public Bid { get; set; }
    ForeignKey("Bid");
    public B B { get; set; }
}

[Table("D")]
public class D : C
{
    public Cid { get; set;}
    ForeignKey("Cid");
    public C C { get; set; }
}

How can I make one table with a foreign key for each class or any other possible correct way? Maybe someone could post some sample how to achieve it?

Maybe this is trivial question but I've spent whole day, implemented many solutions described in topics and it still not work. So I'll be very grateful if someone could help me with that.

解决方案

In mapped inheritance you don't have accessible navigation property to parent entity unless you want to model another relation. Inheritance is "is a" relation so B is A while FK is "has a" relation.

Here you have a great walkthrough for TPT inheritance.

这篇关于如何通过代码优先方法实现实体框架4.3中的继承表类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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