实体框架将数据保存在一对一关联中

发布于 2024-11-06 00:04:36 字数 1583 浏览 0 评论 0原文

我正在尝试使用 外键关联方法在 EF 中实现一对一关联。就我而言,用户和团队之间存在关联,并且我需要在每个用户和团队中都有一个导航属性。 我在尝试保存数据时遇到了问题。

这就是模型的样子:

public class Team
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int OwnerID { get; set; }
    public virtual User Owner { get; set; }
}

public class User
{
    public int ID { get; set; }
    public string UserName { get; set; }

    public int TeamID { get; set; }
    public virtual Team Team { get; set; }
}

我将这些位添加到 DbContext OnModelCreating() 中,按照上面引用的博客文章中的说明进行操作:

modelBuilder.Entity<User>()
    .HasRequired(u => u.Team)
    .WithMany()
    .HasForeignKey(u => u.TeamID);

modelBuilder.Entity<Team>()
    .HasRequired(t => t.Owner)
    .WithMany()
    .HasForeignKey(t => t.OwnerID)
    .WillCascadeOnDelete(false);

现在添加这样的数据时:

User u = new User();
u.UserName = "farinha";
Team t = new Team("Flour Power");
u.Team = t;
t.Owner = u;
context.Users.Add(u);
context.Teams.Add(t);
context.SaveChanges();

甚至像这样:

User u = new User();
u.UserName = "farinha";
u.Team = new Team("Flour Power");
context.Users.Add(u);
context.SaveChanges();

我收到以下错误:

无法确定有效的排序 对于相关操作。依赖关系 可能由于外键而存在 约束、模型要求或 存储生成的值。

知道如何解决这个问题吗?我保存数据的方式是否错误?

提前致谢

I'm trying to use the foreign key association approach to achieve a one-to-one association in EF. In my case there's an association between a User and a Team, and I need a navigation property in each of them.
I bumped into a problem when trying to save data.

This is what the models look like:

public class Team
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int OwnerID { get; set; }
    public virtual User Owner { get; set; }
}

public class User
{
    public int ID { get; set; }
    public string UserName { get; set; }

    public int TeamID { get; set; }
    public virtual Team Team { get; set; }
}

I added these bits to the DbContext OnModelCreating(), as instructed in the blog post referenced above:

modelBuilder.Entity<User>()
    .HasRequired(u => u.Team)
    .WithMany()
    .HasForeignKey(u => u.TeamID);

modelBuilder.Entity<Team>()
    .HasRequired(t => t.Owner)
    .WithMany()
    .HasForeignKey(t => t.OwnerID)
    .WillCascadeOnDelete(false);

And now when adding data like this:

User u = new User();
u.UserName = "farinha";
Team t = new Team("Flour Power");
u.Team = t;
t.Owner = u;
context.Users.Add(u);
context.Teams.Add(t);
context.SaveChanges();

or even like this:

User u = new User();
u.UserName = "farinha";
u.Team = new Team("Flour Power");
context.Users.Add(u);
context.SaveChanges();

I'm getting the following error:

Unable to determine a valid ordering
for dependent operations. Dependencies
may exist due to foreign key
constraints, model requirements, or
store-generated values.

Any idea of how to solve this? Am I saving the data in a wrong way?

Thanks in advance

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

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

发布评论

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

评论(1

酒绊 2024-11-13 00:04:36

您没有以错误的方式保存数据,但它根本无法工作,因为您定义了双向依赖关系。仅当与已保存的用户相关时才能保存团队,并且仅当与现有团队相关时才能保存用户。您必须通过将外键属性标记为可为空来使一个关系成为可选(例如 User 中的 TeamId):

public class User
{
    public int ID { get; set; }
    public string UserName { get; set; }

    public int? TeamID { get; set; }
    public virtual Team Team { get; set; }
}

然后您必须首先保存 User 实体并然后你就可以保存团队

User u = new User();
u.UserName = "farinha";
context.Users.Add(u);
context.SaveChanges();

u.Team = new Team { Name = "Flour Power", OwnerID = u.ID };
context.SaveChanges();

You are not saving data wrong way but it simply cannot work because you defined bidirectional dependency. Team can be saved only if related to already saved user and user can be saved only if related to existing team. You must make one relation optional by marking foreign key property nullable (for example TeamId in User):

public class User
{
    public int ID { get; set; }
    public string UserName { get; set; }

    public int? TeamID { get; set; }
    public virtual Team Team { get; set; }
}

Then you must save the User entity first and then you can save Team.

User u = new User();
u.UserName = "farinha";
context.Users.Add(u);
context.SaveChanges();

u.Team = new Team { Name = "Flour Power", OwnerID = u.ID };
context.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文