One-to-Zero/One 和 Entity Framework 中的一对多关系 5
Both One-to-Zero/One and One-to-Many relation in Entity Framework 5
我 google 很苦恼,但无法完全理解,here 我发现有用的信息很少,但没有成功。下面是我的问题,
我有以下 类 并且需要一种方法来指定实体模型中的关系。
public class User
{
[Key]
public int UserId { get; set; }
public int? HomeAddressId { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual Address HomeAddress{ get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public int UserId { get; set; }
public string AddressLine1 { get; set; }
// other properties
public virtual User User { get; set; }
}
要求是每个用户有多个地址,每个地址与一个用户关联(一对多)。
地址之一是家庭地址,因此用户有家庭地址(可选)。 HomeAddressId 是地址 table 的 FK(可为空)。我如何首先在实体模型代码中定义这种关系,最好是在 Fluent API 方法中?
我更喜欢以简单的方式展示代码。
public class User
{
[Key]
public int UserId { get; set; }
public int? HomeAddressId { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public Address HomeAddress {
get {
if(Addresses != null)
return Addresses.FirstOrDefault(p=> p.AddressType.Home);
return null;
}
}
}
public enum AddressType
{
Unknown,
Home,
Office
}
public class Address
{
[Key]
public int AddressId { get; set; }
public int UserId { get; set; }
public string AddressLine1 { get; set; }
public AddressType AddressType {get; set; }
// other properties
public virtual User User { get; set; }
}
第二流利。
public class User
{
public int UserId { get; set; }
public virtual Address HomeAddress { get; set; }
public virtual int? HomeAddressId { get; set; }
public virtual ICollection<Address> OfficeAddresses { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
}
public class UserMap : MapEntityTypeConfiguration<User>
{
public UserMap ()
{
//Other definitions PK etc...
this.HasOptional(o => o.HomeAddress)
.WithMany()
.HasForeignKey(a => a.HomeAddressId);
this.HasMany(t => t.OfficeAddresses)
.WithMany()
.Map(m =>
{
m.ToTable("UserOfficeAddresses");
m.MapLeftKey("AddressId");
m.MapRightKey("UserId");
});
}
}
//and define Address Configurations.. MapEntityTypeConfiguration<Address>
我 google 很苦恼,但无法完全理解,here 我发现有用的信息很少,但没有成功。下面是我的问题,
我有以下 类 并且需要一种方法来指定实体模型中的关系。
public class User
{
[Key]
public int UserId { get; set; }
public int? HomeAddressId { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual Address HomeAddress{ get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public int UserId { get; set; }
public string AddressLine1 { get; set; }
// other properties
public virtual User User { get; set; }
}
要求是每个用户有多个地址,每个地址与一个用户关联(一对多)。 地址之一是家庭地址,因此用户有家庭地址(可选)。 HomeAddressId 是地址 table 的 FK(可为空)。我如何首先在实体模型代码中定义这种关系,最好是在 Fluent API 方法中?
我更喜欢以简单的方式展示代码。
public class User
{
[Key]
public int UserId { get; set; }
public int? HomeAddressId { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public Address HomeAddress {
get {
if(Addresses != null)
return Addresses.FirstOrDefault(p=> p.AddressType.Home);
return null;
}
}
}
public enum AddressType
{
Unknown,
Home,
Office
}
public class Address
{
[Key]
public int AddressId { get; set; }
public int UserId { get; set; }
public string AddressLine1 { get; set; }
public AddressType AddressType {get; set; }
// other properties
public virtual User User { get; set; }
}
第二流利。
public class User
{
public int UserId { get; set; }
public virtual Address HomeAddress { get; set; }
public virtual int? HomeAddressId { get; set; }
public virtual ICollection<Address> OfficeAddresses { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
}
public class UserMap : MapEntityTypeConfiguration<User>
{
public UserMap ()
{
//Other definitions PK etc...
this.HasOptional(o => o.HomeAddress)
.WithMany()
.HasForeignKey(a => a.HomeAddressId);
this.HasMany(t => t.OfficeAddresses)
.WithMany()
.Map(m =>
{
m.ToTable("UserOfficeAddresses");
m.MapLeftKey("AddressId");
m.MapRightKey("UserId");
});
}
}
//and define Address Configurations.. MapEntityTypeConfiguration<Address>