我使用以下几个实体:
public class Store {
public int Id { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Promotion> Promotions { get; set; }
}
public class Product {
[product properties here...]
}
public class Promotion : Product {
[extra promotion properties here...]
}首先使用代码生成我的数据库。它生成一个Products表,其中包含一个判别器(以区分它正在存储的类的类型),然后生成一个Store_Id外键,当它是一个产品时填充,在使用促销时使用另一个Store_Id1。
现在,过了一段时间,我需要通过特定的促销访问商店。怎么做呢?我试着用流利的语言告诉它外国钥匙的事,但我失败了。
你们能给我点线索吗?
谢谢,
发布于 2015-12-14 12:20:32
这不是一段简单的一对一的关系吗?
public int StoreID { get; set; }
public virtual Store Store { get; set; }
public int Store1ID { get; set; }
public virtual Store Store1 { get; set; }
modelBuilder.Entity<Store>()
.HasMany(m => m.Products)
.WithRequired(m => m.Store);
modelBuilder.Entity<Store>()
.HasMany(m => m.Promotions)
.WithRequired(m => m.Store1);然后,Product.Store和Product.Store1将只是导航属性返回到它们的存储区。
发布于 2015-12-14 12:20:29
我相信你的模特儿可以改进..。Promotion不应该扩展Product。你应该把它们解耦,让促销点指向一个产品和一个商店(如果你想要商店特定的促销).意味着Product和Store应该是Promotion实体中的一个字段.进一步拓展一下,想象一下,如果你想要一次涉及多个产品的单一促销活动(例如:肥皂和洗发水、跑鞋和袜子等),那会是什么样子?
https://stackoverflow.com/questions/34265110
复制相似问题