【发布时间】:2011-10-26 12:47:47
【问题描述】:
假设您有以下模型:
public class Dog {
public int DogId { get; set; }
public string Name { get; set; }
}
public class Cat {
public int CatId { get; set; }
public string Name { get; set; }
}
// This model/table allows us to link multiple colors to an animal
// Lets say type of 1 is dog, 2 is cat for simplicity
public class AnimalColor {
public int ObjectId { get; set; }
public int TypeId { get; set; }
public virtual Color Color { get; set; }
}
public class Color {
public int ColorId { get; set; }
public string Description { get; set; }
}
这种架构的问题在于,AnimalColor 在技术上是 Dog 和 Cat 的导航属性,但它的复杂性使您无法使用“内置”功能,例如 AnimalColor 和 Color 之间的关系。
Dog和AnimalColor之间的关系有一个TypeId的条件,更何况ForeignKey不能正常工作,因为键名不一样(DogId和ObjectId)。
我的问题是:我是否完全错过了使这项工作有效的东西?如果不是,如果我想提取以 AnimalColors 作为属性的 Dogs 列表,那么处理这种情况的最佳方法是什么?
目前我对此唯一的解决方案是拉两个列表并在我循环遍历 Dogs 时抓取颜色。似乎应该有更优雅的方式。
【问题讨论】:
标签: asp.net-mvc-3 models relationships navigation-properties