EF Code First - 流畅的 API(WithRequiredDependent 和 WithRequiredPrincipal)

发布于 2024-12-09 14:32:54 字数 933 浏览 0 评论 0原文

我有以下课程:

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Couple Couple { get; set; }
}

public class Couple
{
    public Guid Id { get; set; }
    public User Groom { get; set; }
    public User Bride { get; set; }
}

要点:

  1. BrideGroom 属性是必需的
  2. 一对一
  3. 关系User类,它是Couple必需的

OnModelCreating中的DbContext

modelBuilder.Entity<User>().HasRequired(u => u.Couple).WithRequiredPrincipal();
modelBuilder.Entity<Couple>().HasRequired(u => u.Bride).WithRequiredDependent();
modelBuilder.Entity<Couple>().HasRequired(u => u.Groom).WithRequiredDependent();

但我不能被要求!

数据库中所有字段均为空!。

如何获取数据库中的字段不为空? 如果可能,请使用 API Flient

I have the following class:

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Couple Couple { get; set; }
}

public class Couple
{
    public Guid Id { get; set; }
    public User Groom { get; set; }
    public User Bride { get; set; }
}

Important points:

  1. Bride and Groom properties are required
  2. One-to-one relationship
  3. In the User class, it is Couple required

DbContext in OnModelCreating

modelBuilder.Entity<User>().HasRequired(u => u.Couple).WithRequiredPrincipal();
modelBuilder.Entity<Couple>().HasRequired(u => u.Bride).WithRequiredDependent();
modelBuilder.Entity<Couple>().HasRequired(u => u.Groom).WithRequiredDependent();

But I can not be required!

All fileds are with null in the database!.

How do I get the fields in the database as not null?
If possible using the API Flient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-12-16 14:32:54

应该是这样的:

modelBuilder.Entity<User>().HasRequired(u => u.Couple).WithRequiredDependent();
modelBuilder.Entity<Couple>().HasRequired(u => u.Bride).WithRequiredDependent();
modelBuilder.Entity<Couple>().HasRequired(u => u.Groom).WithRequiredDependent();

WithRequiredDependent 的工作原理:将关系配置为 required:required,而在关系的另一端没有导航属性。正在配置的实体类型将是从属实体类型,并包含主体的外键。实体
输入关系目标将成为关系中的主体。


含义:让我们考虑一下这里的第一行代码。它在正在配置的实体(用户)中创建一个外键,使其依赖并使关系的另一方(耦合校长


重要:您不认为您想要的配置会产生死锁吗?我没有测试上面的代码,但这个配置对我来说似乎是一个僵局,所以我不确定 EF 是否允许您创建它。用户必须需要一对,我猜一对夫妇必须需要同一个用户。

It should be this :

modelBuilder.Entity<User>().HasRequired(u => u.Couple).WithRequiredDependent();
modelBuilder.Entity<Couple>().HasRequired(u => u.Bride).WithRequiredDependent();
modelBuilder.Entity<Couple>().HasRequired(u => u.Groom).WithRequiredDependent();

How WithRequiredDependent Works : Configures the relationship to be required:required without a navigation property on the other side of the relationship. The entity type being configured will be the dependent and contain a foreign key to the principal. The entity
type that the relationship targets will be the principal in the relationship.


Meaning : Let's consider your first line of code here. It creates a foreign key in the entity being configured (User) making it Dependant and making the other side of the relationship (Couple) Principal


Important : Don't you think the configuration you desire will generate a deadlock? I've not tested the code above, but this configuration seems to be a deadlock to me so i'm not sure if EF would allow you to create it. User must need a Couple, and Couple must need that same user i guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文