简体   繁体   English

实体框架关系匹配-数据库中没有外键

[英]Entity Framework Relationship Matching - No Foreign Keys In DB

I've gone through the Entity Framework code-first tutorials and I hate to say but I'm at a loss for how it works. 我已经完成了Entity Framework代码优先教程,但我不想说,但是我对它的工作方式一无所知。 Everything works but I want to know how because everything I know about basic database design tells me it shouldn't. 一切正常,但是我想知道如何做,因为我对基本数据库设计的一切了解都告诉我不应该。 I feel like I'm missing a critical link with how it's creating foreign keys and hope someone can explain it. 我觉得我缺少关于如何创建外键的关键链接,希望有人能解释一下。

I initially copied and pasted a bunch of code-first designs of a simple Parent class with multiple child types. 我最初复制并粘贴了具有多个子类型的简单Parent类的大量代码优先设计。 In my child types I had a reference to the virtual Parent and a ParentID reference. 在我的子类型中,我有一个对虚拟父ParentID引用和一个ParentID引用。 I noticed in the database that the value of ParentID was 0 for every single child of type A and B. 我在数据库中注意到,对于类型A和B的每个孩子, ParentID的值为0。

However, when loading the data in code and debugging/displaying, entity relationships were correctly maintained. 但是,在代码中加载数据并进行调试/显示时,正确维护了实体关系。

The code looks like (class names were changed to remove context of what it is): 代码看起来像(更改了类名以删除其上下文):

public class Parent{
    public int? ParentID {get;set;}
    public virtual ChildA ChildA {get;set;}
    public virtual ChildB ChildB {get;set;}
}

public class ChildA{
    [Key,ForeignKey(Parent)]
    public int ChildAID {get;set;}

    public string Field {get;set;}

    //public int ParentID {get;set;}

    [ForeignKey("ParentID")]
    public virtual Parent Parent {get;set;}
}

public class ChildB{
    [Key,ForeignKey(Parent)]
    public int ChildBID {get;set;}

    public string Field {get;set;}

    //public int ParentID {get;set;}

    [ForeignKey("ParentID")]
    public virtual Parent Parent {get;set;}
}

I commented out the ParentID to show explicitly what I was talking about. 我注释掉了ParentID以明确显示我在说什么。 To test this out, I deleted the column in the database and yet the relationships still work correctly. 为了对此进行测试,我删除了数据库中的列,但是关系仍然可以正常工作。 There are only three tables that relate to this in the database (Parent, ChildA, and ChildB). 数据库中只有三个与此表相关的表(Parent,ChildA和ChildB)。 There aren't any foreign keys, and the parent table doesn't reference the children tables and the children tables don't reference the parent table. 没有任何外键,并且父表不引用子表,并且子表不引用父表。 When I load them from the DBContext , the mappings correctly load them as I saved them. 当我从DBContext加载它们时,映射在保存它们时正确加载了它们。

What am I missing here? 我在这里想念什么? Something somewhere is keeping track of this mapping but I can't tell where. 某处正在跟踪此映射,但我不知道在哪里。 And the fact that it isn't stored as a really basic foreign key in the database concerns me about performance. 而且它没有作为真正的基本外键存储在数据库中,这一事实使我担心性能。

What do you mean by There aren't any foreign keys, and the parent table doesn't reference the children tables and the children tables don't reference the parent table. 您的意思There aren't any foreign keys, and the parent table doesn't reference the children tables and the children tables don't reference the parent table. ?

I think you were missing to notice that there are relationship between them. 我认为您没有注意到它们之间存在关系。

桌子

It's called one to zero-or-one relationship. 这称为一对零或一对一关系。

Your question is a bit baffling because Entity Framework does store relationships as "really basic foreign keys", so I can't see how you've come to that conclusion. 您的问题有点困惑,因为Entity Framework确实将关系存储为“真正的基本外键”,所以我看不到您如何得出该结论。

Commenting out the ParentID to show what your talking about doesn't help much because it should actually have had no effect. 注释掉ParentID以显示您在说什么并没有多大帮助,因为它实际上应该没有任何作用。 That is because when you add the [ForeignKey("ParentID")] attribute to the Parent property, you are telling Entity Framework to put a foreign key in the database named "ParentID" to model the Parent relationship. 那是因为当您将[ForeignKey(“ ParentID”)]属性添加到Parent属性时,您是在告诉Entity Framework将外键放在名为“ ParentID”的数据库中以建模Parent关系。

You don't need the ParentID integer property in your Entity, but it is usually easier to include foreign keys, and most examples show this. 您不需要在Entity中使用ParentID整数属性,但是通常更容易包含外键,大多数示例都显示了这一点。

Now, if you do include ParentID, then you don't need the [ForeignKey("ParentID")] attribute on the Parent property because EF will assume that ParentID is the foreign key for the Parent navigation property because you have followed a standard naming convention. 现在,如果您确实包含ParentID,那么您就不需要Parent属性的[ForeignKey(“ ParentID”)]属性,因为EF会假定ParentID是Parent导航属性的外键,因为您遵循的是标准命名惯例。

Also you could try this - remove both the ParentID and the [ForeignKey("ParentID"] attribute and update the database. Now you should find that EF has added a foreign key named "Parent_Id". So foreign keys used always! 您也可以尝试-删除ParentID和[ForeignKey(“ ParentID”]属性并更新数据库。现在您应该发现EF添加了一个名为“ Parent_Id”的外键,因此外键始终使用!

The [Key,ForeignKey(Parent)] attribute on your keys looks wrong. 您的键上的[Key,ForeignKey(Parent)]属性看起来不正确。 The ID isn't a navigation property so you don't need to specify a foreign key for it, so I'm not sure what that is doing (actually, on second thoughts, this may be your problem, I think this is specifying a one to one). 该ID不是导航属性,因此您无需为其指定外键,因此我不确定该在做什么(实际上,再三考虑,这可能是您的问题,我认为这是在指定一对一)。 That Key attribute is also unnecessary because again you have followed a naming convention. 该Key属性也是不必要的,因为您再次遵循了命名约定。

So in summary, get shot of all your attributes and follow the naming conventions until you need to do something tricky! 因此,总而言之,请掌握所有属性并遵循命名约定,直到您需要做一些棘手的事情为止!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM