无法确定关联的原则结束 [英] Unable to determine the principle end of an association
问题描述
[Table(UserProfile)]
应该总是有一个
public class UserProfile {
[Key]
[DatabaseGeneratedOption.Identity]]
public int UserId {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public DateTime DateOfBirth {get;组; }
[ForeignKey(AddressId)]
public virtual Address Address {get;组; }
$ b $ {
$ b $ [b
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddressId {get;组; }
public string Address1 {get;组; }
public string Address2 {get;组; }
public string City {get;组; }
public virtual State State {get;组; }
public string ZipCode {get;组; }
$ b $ [ForeignKey(UserId)]
public virtual UserProfile User {get;组; }
code $ $ $ $ $ $ $ $ $ $ UserProfile地址
但我也想在地址
中有一个导航属性,这样我就可以通过地址查找用户。所以,一旦迁移完成了它的事情,我想这些表看起来像...UserProfile
UserId(PK)
...
地址
AddressId(PK)
...
UserId(FK)
在包管理器控制台中,我正在运行
update-database
下面的消息...
blockquote
无法确定类型TastySerpent.Domain.Models.Address和'TastySerpent.Domain.Models.UserProfile'。这个关联的主要目的必须是使用关系流畅API或数据注释来显式配置。
我很困惑如何在Entity Framework 5中建立一对一的关系。
解决方案 你映射:
$ b $ pre $modelBuilder.Entity< Bar>()
.HasOptional(f => f.Baz) 。 // Baz是依赖的并得到一个FK BarId
.WithRequired(s => s.Bar); // Bar是principal
modelBuilder.Entity< Baz>()
.Has可选(f => f.Bar)。 // Bar是依赖的并且获得FK BazId
.WithRequired(s => s.Baz); // Baz是主体
依赖项获取引用委托人密钥的外键。当它是一对一的时候,那个外键也是依赖的主键,但是EF不能确定哪个是哪个,这就是为什么在你指定它之前得到一个错误。
参考文献:
http://msdn.microsoft.com/en-us/library/ee382827.aspx
https://stackoverflow.com/a/19580798/150342
Using EF5 Code first, I have two classes:
[Table("UserProfile")] public class UserProfile { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } [ForeignKey("AddressId")] public virtual Address Address { get; set; } } [Table("Address")] public class Address : IEntity { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int AddressId { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public virtual State State { get; set; } public string ZipCode { get; set; } [ForeignKey("UserId")] public virtual UserProfile User { get; set; } }
UserProfile
should always have anAddress
but I also wanted to have a navigation property inAddress
so that I could look up users by address. So, once migrations has done its thing, I'd like the tables to look like...UserProfile UserId (PK) ... Address AddressId (PK) ... UserId (FK)
From the Package Manager Console, I'm running
update-database
and getting the following message...Unable to determine the principal end of an association between the types 'TastySerpent.Domain.Models.Address' and 'TastySerpent.Domain.Models.UserProfile'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I'm confused on how to set up one-to-one relationships in Entity Framework 5.
解决方案You define the principal when you map:
modelBuilder.Entity<Bar>() .HasOptional(f => f.Baz). //Baz is dependent and gets a FK BarId .WithRequired(s => s.Bar);//Bar is principal modelBuilder.Entity<Baz>() .HasOptional(f => f.Bar). //Bar is dependent and gets a FK BazId .WithRequired(s => s.Baz);//Baz is principal
The dependent gets the foreign key that references the principal's key. When it's a one to one, that foreign key is also the dependent's primary key but EF can't work out which is which and that's why you get an error until you've specified it.
References:
http://msdn.microsoft.com/en-us/library/ee382827.aspx
https://stackoverflow.com/a/19580798/150342
这篇关于无法确定关联的原则结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文