无法确定关联的主体结束 - 实体框架模型首先 [英] Unable to determine the principal end of an association - Entity Framework Model First

查看:120
本文介绍了无法确定关联的主体结束 - 实体框架模型首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Visual Studio中创建了实体数据模型。现在我有文件与SQL查询和从模型生成的C#类。



问题:



生成没有注释或代码(Fluent API)的类。是吗?我试图运行我的应用程序,但抛出异常:



无法确定类型Runnection.Models.Address和Runnection.Models之间关联的主体结束。用户'。必须使用关联流畅的API或数据注释来明确地配置此关联的主要端点。



我读到,我无法使用Fluent API与Model First。那我该怎么办?



代码:



用户

  public partial class User 
{
public User()
{
this.Events = new HashSet< Event>();
this.CreatedEvents = new HashSet< Event>();
}

public int Id {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string Photo {get;组; }
public int EventId {get;组; }
public string昵称{get;组; }
public OwnerType OwnerType {get;组; }
public NetworkPlaceType PlaceType {get;组; }

public virtual ICollection< Event>活动{get;组; }
public virtual Address Address {get;组; }
public virtual ICollection< Event> CreatedEvents {get;组; }
public virtual Owner Owner {get;组;
}

地址

  public partial class Address 
{
public int Id {get;组; }
public string Street {get;组; }
public string StreetNumber {get;组; }
public string City {get;组; }
public string ZipCode {get;组; }
public string国家{get;组; }

public virtual User User {get;组;
}

上下文



// Model First不使用此方法

 保护覆盖void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Address>()。HasRequired(address => address.User)
.WithRequiredDependent();
modelBuilder.Entity< User>()。HasRequired(user => user.Address)
.WithRequiredPrincipal();

base.OnModelCreating(modelBuilder);
}


解决方案

您必须指定主体以一对一的关系。

  public partial class Address 
{
[Key,ForeignKey(User)]
public int Id {get;组; }
public string Street {get;组; }
public string StreetNumber {get;组; }
public string City {get;组; }
public string ZipCode {get;组; }
public string国家{get;组; }

public virtual User User {get;组; }
}

通过指定FK约束,EF知道用户必须先存在主体),地址如下。



在MSDN上进一步阅读



另外,请参阅这个SO答案。






从评论更新






在设计师,选择关联(用户和地址之间的行)。在属性窗口中,使用参考约束上的[...]按钮或(或双击该行)。将校长设为用户。





I have created Entity Data Model in Visual Studio. Now I have file with SQL queries and C# classes generated from Model.

Question:

Classes are generated without annotations or code behind (Fluent API). Is it OK? I tried to run my application but exception was thrown:

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

I read that I can not use Fluent API with "Model First". So what can I do?

Code:

User

public partial class User
{
    public User()
    {
        this.Events = new HashSet<Event>();
        this.CreatedEvents = new HashSet<Event>();
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Photo { get; set; }
    public int EventId { get; set; }
    public string Nickname { get; set; }
    public OwnerType OwnerType { get; set; }
    public NetworkPlaceType PlaceType { get; set; }

    public virtual ICollection<Event> Events { get; set; }
    public virtual Address Address { get; set; }
    public virtual ICollection<Event> CreatedEvents { get; set; }
    public virtual Owner Owner { get; set; }
}

Address

public partial class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string StreetNumber { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

    public virtual User User { get; set; }
}

Context

//Model First does not use this method

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>().HasRequired(address => address.User)
                                   .WithRequiredDependent();
        modelBuilder.Entity<User>().HasRequired(user => user.Address)
                                   .WithRequiredPrincipal();

        base.OnModelCreating(modelBuilder);
    }

解决方案

You have to specify the principal in a one-to-one relationship.

public partial class Address
{
    [Key, ForeignKey("User")]
    public int Id { get; set; }
    public string Street { get; set; }
    public string StreetNumber { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

    public virtual User User { get; set; }
}

By specifying a FK constraint, EF knows the User must exists first (the principal) and the Address follows.

Further reading at MSDN.

Also, see this SO answer.


Updated from comments


In the designer, select the association (line between Users & Address). On the properties window, hit the button with the [...] on Referential Constraint (or double click the line). Set the Principal as User.


这篇关于无法确定关联的主体结束 - 实体框架模型首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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