使用实体框架code首先一对一可选的关系 [英] One to one optional relationship using Entity Framework Code First

查看:174
本文介绍了使用实体框架code首先一对一可选的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望使用实体框架code先用一对一的可选的关系。我们有两个实体。

We want to use one to one optional relationship using Entity Framework Code First. We have two entities.

public class PIIUser
{
    public int Id { get; set; }

    public int? LoyaltyUserDetailId { get; set; }
    public LoyaltyUserDetail LoyaltyUserDetail { get; set; }
}

public class LoyaltyUserDetail
{
    public int Id { get; set; }
    public double? AvailablePoints { get; set; }

    public int PIIUserId { get; set; }
    public PIIUser PIIUser { get; set; }
}

PIIUser可以具有LoyaltyUserDetail但LoyaltyUserDetail必须有一个PII用户。
我们尝试这些方法流利的技术。

PIIUser may have a LoyaltyUserDetail but LoyaltyUserDetail must have a PII User. We tried these fluent approach techniques.

modelBuilder.Entity<PIIUser>()
            .HasOptional(t => t.LoyaltyUserDetail).WithOptionalPrincipal(t => t.PIIUser)
            .WillCascadeOnDelete(true);

这个方法并没有创建PIIUsers表LoyaltyUserDetailId外键。

This approach didn't create LoyaltyUserDetailId foreign key in PIIUsers table.

之后,我们尝试了以下code。

After that we tried the following code.

modelBuilder.Entity<LoyaltyUserDetail>().HasRequired(t => t.PIIUser).WithRequiredDependent(t => t.LoyaltyUserDetail);

但这次EF并没有在这2个表创建的任何外键。

But this time EF didn't create any foreign keys in these 2 tables.

你有此问题的任何想法?
我们怎么可以先使用实体​​框架code创建一对一可选的关系?

Do you have any ideas for this issue? How can we create one to one optional relationship using entity framework code first ?

推荐答案

EF code首先支持 1:1 1:0 ..1 的关系。后者是你在找什么(一到零或一)。

EF Code First supports 1:1 and 1:0..1 relationships. The latter is what you are looking for ("one to zero-or-one").

您在流畅的尝试被说的两端在一个案件和可选两端中的其他必需的。

Your attempts at fluent are saying required on both ends in one case and optional on both ends in the other.

您需要的是可选的一端和的需要的另一方。

What you need is optional on one end and required on the other.

下面是从编程E.F. code第一本书的例子

Here's an example from the Programming E.F. Code First book

modelBuilder.Entity<PersonPhoto>()
.HasRequired(p => p.PhotoOf)
.WithOptional(p => p.Photo);

PersonPhoto 实体都有一个称为导航属性 PhotoOf 指向一个类型。在类型有一个名为照片导航属性是指向 PersonPhoto 键入

The PersonPhoto entity has a navigation property called PhotoOf that points to a Person type. The Person type has a navigation property called Photo that points to the PersonPhoto type.

在两个相关的类,您使用的每个类型的的主键的,不是的外键的。即,你不会使用 LoyaltyUserDetailId PIIUserId 属性。相反,关系取决于标识两种类型的字段。

In the two related classes, you use each type's primary key, not foreign keys. i.e., you won't use the LoyaltyUserDetailId or PIIUserId properties. Instead, the relationship depends on the Id fields of both types.

如果您使用的是一口流利的API上面一样,你不需要指定 LoyaltyUser.Id 作为外键,EF会看着办吧。

If you are using the fluent API as above, you do not need to specify LoyaltyUser.Id as a foreign key, EF will figure it out.

因此​​,无需您code测试自己(我恨我头上做这个)...我会翻译成你的code这是

So without having your code to test myself (I hate doing this from my head)... I would translate this into your code as

public class PIIUser
{
    public int Id { get; set; }    
    public LoyaltyUserDetail LoyaltyUserDetail { get; set; }
}

public class LoyaltyUserDetail
{
    public int Id { get; set; }
    public double? AvailablePoints { get; set; }    
    public PIIUser PIIUser { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  modelBuilder.Entity<LoyaltyUserDetail>()
  .HasRequired(lu => lu.PIIUser )
  .WithOptional(pi => pi.LoyaltyUserDetail );
}

这是说LoyaltyUserDetails PIIUser 属性的需要的和PIIUser的 LoyaltyUserDetail 属性是可选的。

That's saying LoyaltyUserDetails PIIUser property is required and PIIUser's LoyaltyUserDetail property is optional.

您可以从另一端开始:

modelBuilder.Entity<PIIUser>()
.HasOptional(pi => pi.LoyaltyUserDetail)
.WithRequired(lu => lu.PIIUser);

现在说PIIUser的 LoyaltyUserDetail 属性是可选的,需要LoyaltyUser的 PIIUser 属性。

which now says PIIUser's LoyaltyUserDetail property is optional and LoyaltyUser's PIIUser property is required.

您总是要使用该模式HAS /带。

You always have to use the pattern HAS/WITH.

HTH和FWIW,一对一(或一个零/个)的关系是最令人困惑的关系,code,所以你并不孤单配置第一个! :)

HTH and FWIW, one to one (or one to zero/one) relationships are one of the most confusing relationships to configure in code first so you are not alone! :)

这篇关于使用实体框架code首先一对一可选的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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