什么是协会的主要终点是指在1:在实体框架1的关​​系 [英] What does principal end of an association means in 1:1 relationship in Entity framework

查看:189
本文介绍了什么是协会的主要终点是指在1:在实体框架1的关​​系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Foo
{
    public string FooId{get;set;}
    public Boo Boo{get;set;}
}


public class Boo
{
    public string BooId{get;set;}
    public Foo Foo{get;set;}
}

我试图做到这一点在实体框架时,我得到了错误:

I was trying to do this in Entity Framework when I got the error:

无法确定类型之间的关联的主要终点
  ConsoleApplication5.Boo'和'ConsoleApplication5.Foo。
  该协会的主要端必须使用显式配置无论是
  关系流利的API或数据注解。

Unable to determine the principal end of an association between the types 'ConsoleApplication5.Boo' and 'ConsoleApplication5.Foo'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

我看到的计算器使用的问题这个错误的解决方案,但我想明白的术语主要结束的意思。

推荐答案

在一到一个关系一端必须是委托人和第二端必须依赖。主要端是将被首先插入,并未经从属可以存在一个。取决于最终是必须本金后插入,因为它有外键校长之一。

In one-to-one relation one end must be principal and second end must be dependent. Principal end is the one which will be inserted first and which can exist without the dependent one. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal.

在实体框架FK的情况下,依赖也必须是它的PK让你的情况,你应该使用:

In case of entity framework FK in dependent must also be its PK so in your case you should use:

public class Boo
{
    [Key, ForeignKey("Foo")]
    public string BooId{get;set;}
    public Foo Foo{get;set;}
}

或者流利的映射

modelBuilder.Entity<Foo>()
            .HasOptional(f => f.Boo)
            .WithRequired(s => s.Foo);

这篇关于什么是协会的主要终点是指在1:在实体框架1的关​​系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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