实体框架代码首先使用Fluent API映射外键 [英] Entity Framework Code First Mapping Foreign Key Using Fluent API
问题描述
public class User
{
public int Id {get;组; }
public int? DefaultAddressId {get;组; }
[ForeignKey(DefaultAddressId)]
public virtual Address DefaultAddress {get;组; }
public virtual ICollection< Address>地址{get;组; $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ p $想删除 public virtual Address DefaultAddress {get;组; }
总之,保持DefaultAddressId并使用Fluent API进行映射,因为当前设置会导致很多麻烦(在这个和其他类中,我有类似的设置)。那么可以使用流畅的api来实现吗?
更新:
地址类目前没有任何引用用户类,这是一个单向的关系。但是,一个地址只属于一个用户,这不是很多关系。这是地址类:
public class Address
{
public int Id {get;组; }
public string Name {get;组; }
public string Details {get;组; }
public virtual Area Area {get;组; }
}
解决方案我会亲自移动外键关系从用户
到地址
,并添加一个 IsDefaultAddress
property on the address class。
public class Address
{
public int Id {get;组; }
//此属性标记FK关系
public virtual User User {get;组; }
public string Name {get;组; }
public string Details {get;组; }
public virtual Area Area {get;组; }
//此属性指示这是否是用户的默认地址
public bool IsDefaultAddress {get;组; }
}
EF将知道需要一个外键
地址
和用户
之间的关系。
这将简化您的模型很多。也就是说,如果一个地址只能属于一个用户(如Slauma在评论中提出的)。
I have the situation where a User can have several addresses. Accordingly, I have an ICollection on my user class. But I also want the user to be able to choose a default address. So I've done the following:
public class User
{
public int Id { get; set; }
public int? DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public virtual Address DefaultAddress { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
//properties were removed for purpose of this post
}
I would like to remove the public virtual Address DefaultAddress { get; set; }
altogether, keep the DefaultAddressId and map it using the Fluent API instead because the current setup is causing a lot of trouble (in this and other classes where I have a similar setup). So can this be done using the fluent api?
UPDATE:
The address class currently doesn't have any reference to the User class, it's a uni-directional relationship. But yes, an address belongs to only ONE user, it's not a many to many relationship. Here's the address class:
public class Address
{
public int Id { get; set; }
public string Name { get; set; }
public string Details { get; set; }
public virtual Area Area { get; set; }
}
解决方案 I would personally move the Foreign Key relation from User
to Address
, and add an IsDefaultAddress
property on the address class.
public class Address
{
public int Id { get; set; }
// This property marks the FK relation
public virtual User User { get; set; }
public string Name { get; set; }
public string Details { get; set; }
public virtual Area Area { get; set; }
// This property signals whether this is the user's default address
public bool IsDefaultAddress { get; set; }
}
EF will know that it needs a Foreign Key
relation between Address
and User
.
This would simplify your model a great deal. That is, of course, if an address can only belong to one user (as asked by Slauma in the comments).
这篇关于实体框架代码首先使用Fluent API映射外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!