使用Entity Framework Fluent API一对一的可选关系 [英] One to one optional relationship using Entity Framework Fluent API

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

问题描述

我们想通过Entity Framework Code First使用一对一的可选关系.我们有两个实体.

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必须具有PIIUser. 我们尝试了这些流畅的方法.

PIIUser may have a LoyaltyUserDetail but LoyaltyUserDetail must have a PIIUser. 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.

之后,我们尝试了以下代码.

After that we tried the following code.

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

但是这次,EF没有在这两个表中创建任何外键.

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

您对此问题有任何想法吗? 我们如何使用实体框架流利的api创建一对一的可选关系?

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

推荐答案

EF Code First支持1:11: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.代码第一书》中的一个示例

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的导航属性,该导航属性指向Person类型. Person类型具有一个称为Photo的导航属性,该属性指向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.

在两个相关的类中,您使用每种类型的主键,而不是外键.即,您将不会使用LoyaltyUserDetailIdPIIUserId属性.而是,关系取决于两种类型的Id字段.

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.

如果您正在使用上述的fluent 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.

因此,无需您的代码来测试自己(我讨厌从头开始)...我会将其翻译为您的代码

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/WITH.

You always have to use the pattern HAS/WITH.

HTH和FWIW,一对一(或一对一/零/一)关系是最容易在代码中配置的关系之一,因此您并不孤单! :)

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! :)

这篇关于使用Entity Framework Fluent API一对一的可选关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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