代码优先的一对多关系 Entity Framework
One-to-many relationships with code-first Entity Framework
我正在尝试使用代码优先设置一对多关系(我认为)Entity Framework。
我有一个 ProductListing
对象,它可以有许多关联的 Country
对象。我以为我已经正确设置了它,但我的 Countries
table 有一个 ProductListing
的参考列。这是一个问题,因为尽管 ProductListing
可以与 Country
建立关系,但 Country
对象不应该知道或依赖 ProductListing
对象。
产品列表
public class ProductListing : AuditedEntity<long>
{
public long ProductId { get; set; }
public Product Product { get; set; }
public long RetailerId { get; set; }
public Retailers.Retailer Retailer { get; set; }
public ICollection<Countries.Country> Countries { get; set; }
public bool IsOfficial { get; set; }
public decimal CurrentPrice { get; set; }
public decimal PreviousPrice { get; set; }
public string ListingUrl { get; set; }
public string AffiliateString { get; set; }
public DateTime? ReleaseDate { get; set; }
public DateTime? ShipDate { get; set; }
public DateTime? ExpiresDate { get; set; }
}
国家/地区
public class Country : AuditedEntity<long>
{
[Required]
[StringLength(ShylockConsts.MaxNameLength)]
public virtual string Name { get; set; }
[Required]
[StringLength(ShylockConsts.MaxIsoLength)]
public virtual string IsoCode { get; set; }
}
您实际拥有的是一个 many-to-many
关系,其中一个产品可以有多个国家,一个国家可以有多个产品。
要对此建模,您将需要一个中间连接 table 来保存每个产品的国家/地区。
例如
public class ProductCountry
{
[Required]
public virtual long ProductID { get; set; }
[Required]
public virtual long CountryID { get; set; }
}
添加一个集合导航 属性 到 Country
实体以及建模一个 many-to-many 关系,如@Steve 所说(但不需要为连接创建实体table在概念模型中)
public class ProductList : ...
{
public ICollection<Country> Countries { get; set; }
}
public class Country : ...
{
public ICollection<ProductListing> ProductListings { get; set; }
}
我正在尝试使用代码优先设置一对多关系(我认为)Entity Framework。
我有一个 ProductListing
对象,它可以有许多关联的 Country
对象。我以为我已经正确设置了它,但我的 Countries
table 有一个 ProductListing
的参考列。这是一个问题,因为尽管 ProductListing
可以与 Country
建立关系,但 Country
对象不应该知道或依赖 ProductListing
对象。
产品列表
public class ProductListing : AuditedEntity<long>
{
public long ProductId { get; set; }
public Product Product { get; set; }
public long RetailerId { get; set; }
public Retailers.Retailer Retailer { get; set; }
public ICollection<Countries.Country> Countries { get; set; }
public bool IsOfficial { get; set; }
public decimal CurrentPrice { get; set; }
public decimal PreviousPrice { get; set; }
public string ListingUrl { get; set; }
public string AffiliateString { get; set; }
public DateTime? ReleaseDate { get; set; }
public DateTime? ShipDate { get; set; }
public DateTime? ExpiresDate { get; set; }
}
国家/地区
public class Country : AuditedEntity<long>
{
[Required]
[StringLength(ShylockConsts.MaxNameLength)]
public virtual string Name { get; set; }
[Required]
[StringLength(ShylockConsts.MaxIsoLength)]
public virtual string IsoCode { get; set; }
}
您实际拥有的是一个 many-to-many
关系,其中一个产品可以有多个国家,一个国家可以有多个产品。
要对此建模,您将需要一个中间连接 table 来保存每个产品的国家/地区。
例如
public class ProductCountry
{
[Required]
public virtual long ProductID { get; set; }
[Required]
public virtual long CountryID { get; set; }
}
添加一个集合导航 属性 到 Country
实体以及建模一个 many-to-many 关系,如@Steve 所说(但不需要为连接创建实体table在概念模型中)
public class ProductList : ...
{
public ICollection<Country> Countries { get; set; }
}
public class Country : ...
{
public ICollection<ProductListing> ProductListings { get; set; }
}