MVC3 模型和复杂的关系

发布于 2024-12-12 06:50:19 字数 906 浏览 0 评论 0原文

假设您有以下模型:

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 之间的关系和颜色。

Dog 和 AnimalColor 之间的关系有一个 TypeId 条件,更不用说外键将无法正常工作,因为键名称不同(DogId 和 ObjectId)。

我的问题是:我是否完全错过了使这项工作有效的东西?如果不是,如果我想提取以 AnimalColors 作为属性的狗列表,处理这种情况的最佳方法是什么?

目前,我对此唯一的解决方案是拉两个列表并在循环遍历狗时抓取颜色。看来应该有更优雅的方式。

Lets say you have the following models:

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; }
}

The problem with this architecture is that AnimalColor is technically a navigation property of both Dog and Cat, but the complexity of it stops you from being able to use the "built in" features like the relationship between AnimalColor and Color.

The relationship between Dog and AnimalColor has a condition of TypeId, not to mention the ForeignKey won't properly work because the key names aren't the same (DogId and ObjectId).

My question is this: am I totally missing something that makes this work? If not, what's the best way to handle this type of situation if I want to pull a list of Dogs with the AnimalColors as a property?

Currently the only solution I have for this is to pull two lists and grab the colors as I loop through the Dogs. Seems like there should be a more elegant way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美煞众生 2024-12-19 06:50:19

根据我对你问题的理解,我会这样写“

public class Animal {
  public int ID { get; set; }
  public List<Color> Colors { get; set; }
  public string Name { get; set; }
}
public class Dog : Animal { }

public class Cat : Animal { }

这样你就不需要 TypeId,你可以像这样验证类型:

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));

但我不是100% 确定这是否是您的问题。

From what I understand from your question, I would write like this"

public class Animal {
  public int ID { get; set; }
  public List<Color> Colors { get; set; }
  public string Name { get; set; }
}
public class Dog : Animal { }

public class Cat : Animal { }

This way you don't need TypeId, you can verify the type like this:

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

if you have more mulyiple colors:

a.Colors.Add(new Color(255, 255, 255));
a.Colors.Add(new Color(100, 100, 0));

But I'm not 100% sure if that was your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文