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

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

问题描述

我们希望使用实体框架代码优先来使用一对一的可选关系.我们有两个实体.

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.

您对这个问题有什么想法吗?我们如何使用实体框架 fluent 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").

您对 fluent 的尝试是在一种情况下两端都需要,而在另一种情况下两端都是可选的.

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天全站免登陆