code首先TPT继承 - 我怎样才能提供外键,而不是EF创建父行给我吗? [英] Code First TPT Inheritance - How can I supply the foreign key instead of EF creating the parent row for me?

查看:151
本文介绍了code首先TPT继承 - 我怎样才能提供外键,而不是EF创建父行给我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个基本的继承层次:

I have a basic inheritance hierarchy like this:

public class Parent
{
    public int ParentID {get; set;}

    public string SomeOtherProperty {get; set;}
};

public class Child : Parent
{
    public string SomeChildProperty {get; set;}
};

我有多个子类,我有一个审计对象,它具有基于父类的接口,允许我写的数据库中通用的方式,像这样

I have multiple child classes and I have an Auditor object that has an interface based on the Parent class that allows me to write to the database in a generic way, like this

public class Auditor
{
    public void WriteObject(Parent p)
    {
        using (MyDbContext context = new Context)
        {
            context.Parents.Add(p);
            context.SaveChanges();
        }
    }
}

所以,我可以使用审计像

So I can use Auditor like

auditor.WriteObject(new Child1());
auditor.WriteObject(new Child2());

等等,等等,每当我写一个子对象,EF创建父行中的DB我。

etc, etc, and whenever I write a Child object, EF created the Parent row in the DB for me.

我遇到的问题是在某些情况下(时间也许10%),我想写一个子记录到数据库,并将其链接到现有父行,也就是我要提供的外键。

The problem I'm having is in some cases (maybe 10% of the time), I want to write a Child record to the DB and link it to an existing Parent row, i.e. I want to supply the foreign key.

我想是这样

Child1 child = new Child();
child1.ParentID = existingID;
auditor.WriteObject(child);

但EF忽略我提供的外键,并创造了新的纪录家长对我来说。

But EF ignored the foreign key I supplied and created a new Parent record for me.

反正是有让EF明白,既然父ID的子对象,我想写上已存在,它并不需要插入父行?

Is there anyway to get EF to understand that, since the parent ID already exists on the Child object I'm trying to write, that it doesn't need to insert the parent row?

推荐答案

我有点糊涂根据您的描述,但会尽力回答我想你可能意味着场景:

I am a little confused based on your description, but will try to answer the scenarios I think you might mean:

选项1 :我觉得这就是你以后:你正在创建的父类的对象。后来,你有更多的信息,并希望垂头丧气给适当的子类。这是不可能以这种方式使用EF。一旦对象被作为特定类型创建这是它必须保持。那么你的情况的选项如下:

Option 1: I think this is what you are after: You are creating objects of the parent class. Later, you have more information and want to 'downcast' them to the appropriate subclass. This is not possible in this way with EF. Once the object is created as a specific type that is what it must remain. The option then in your case is the following:

  1. 这是否对象已经存在(ParentType的)
  2. 删除对象(ParentType的)
  3. 在保存对象(ChildType)

请参阅更好的措辞解释相关比我能在这里给出:

See better worded explainations than I could give here:

<一个href="http://stackoverflow.com/questions/2618586/add-child-to-existing-parent-record-in-entity-framework">Add孩子在实体框架现有的父记录

向下转型与实体框架

如果这是您打算做什么我建议,该模型的某些部分正在实施应重新考虑,以避免这些情况。

If this is what you are intending to do I would suggest that some part of the model being implemented should be reconsidered to avoid these situations.

选项2 :你有子元素属于为一组,以父对象(超类的这种情况下)的列表:

Option 2: You have a list of child elements which belong as a group to a parent object (in this case of a super-type):

在这种情况下,你可以加载父,子添加到父,然后保存(在父级别):

In these cases you can load the parent, add the child to the parent and then save (at the parent level):

Child1 child = new Child();
Parent p = _context.Parents.Find(search based on ID)
p.Children.Add(child);
auditor.WriteObject(p);

更新:选项3

根据您的共享父ID的评论:什么你现在说的不是一个TPT实施和你在这里打破了规则。在TPT你有它驻留在父表,该表是所有表的主键为每个对象的单个ID。应该永远在两个子对象(任何类型)具有相同的主键的情况下,并在所有情况下,孩子的主键的的的parentID。使用更少的抽象的术语可能有助于消除混乱:如果父表是动物和孩子表是猫与狗变得明显,猫与狗不能共享一个主键(因为他们是不同类型的我想你可能是试图实现可能是相反的:一个孩子应该包含母公司的一个实例(使用你的问题的术语)

Based on your comment of shared parent id: what you are talking about now is not a TPT implementation and you are breaking a rule here. In TPT you have a single ID which resides in the 'parent' table which is the primary key across all tables for each object. There should never be a case where two child objects (of any type) have the same primary key, and in all cases a childs primary key IS the parentID. Using less abstract terms might help remove the confusion: if the parent table is Animal and the child tables are Cat and Dog it becomes obvious that Cat and dog would not share a primary key (as they are different types. I think what you might be attempting to achieve is possibly the inverse: a Child should contain an instance of parent (using the terminology from your question).

要这样说另一种方式,如果是的parentID它只能在对象的层次结构的一个分支存在的主键,只会齐心协力继承链构成这种对象。例如,如果我有一个像层次

To say this one more way, if parentID is the primary key it will only exist in one branch of the hierarchy of objects, and will only serve to pull together the chain of inheritance which makes up this object. For example if I have an hierarchy like

动物 - > Dog->贵宾犬

Animal->Dog->Poodle

和我创建一个新的狮子狗与ID = 1,我会永远只能有一组具有ID = 1的记录,这将是在每一个动物,犬和贵宾犬的一行。 (与这三个记录是相同的对象贵宾犬),

and I create a new poodle with ID=1, I will only ever have one set of records with ID=1, and it will be one row in each of Animal, Dog and Poodle. (and all three of these records are for the same Poodle object),

这篇关于code首先TPT继承 - 我怎样才能提供外键,而不是EF创建父行给我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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