Asp.net mvc 3 MVC3型号及;复杂关系
假设您有以下型号:Asp.net mvc 3 MVC3型号及;复杂关系,asp.net-mvc-3,models,relationships,navigation-properties,Asp.net Mvc 3,Models,Relationships,Navigation Properties,假设您有以下型号: 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
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在技术上是狗和猫的导航属性,但它的复杂性使您无法使用“内置”功能,如AnimalColor和颜色之间的关系
Dog和AnimalColor之间的关系有一个TypeId条件,更不用说ForeignKey不能正常工作,因为key名称不相同(DogId和ObjectId)
我的问题是:我是否完全错过了让这一切得以实现的东西?如果不是,如果我想列出一份以动物颜色为属性的狗的名单,那么处理这种情况的最佳方法是什么
目前,我唯一的解决办法就是拉两张单子,在我穿过狗的时候抓取颜色。似乎应该有一种更优雅的方式。根据我对你问题的理解,我会这样写“ 如果您有更多多种颜色:
a.Colors.Add(new Color(255, 255, 255));
a.Colors.Add(new Color(100, 100, 0));
但我不能100%确定这是否是你的问题
Cat a = new Cat();
Dog b = new Dog();
Animal c = new Dog();
if (a is Cat) {...} // true
if (b is Dog) {...} // true
if (c is Dog) {...} // true
a.Colors.Add(new Color(255, 255, 255));
a.Colors.Add(new Color(100, 100, 0));