无法确定关联的主体结束 - 实体框架模型首先 [英] Unable to determine the principal end of an association - Entity Framework Model First
问题描述
问题:
生成没有注释或代码(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知道用户必须先存在主体),地址如下。
从评论更新
在设计师,选择关联(用户和地址之间的行)。在属性窗口中,使用参考约束上的[...]按钮或(或双击该行)。将校长设为用户。
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.
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屋!