ClassMap 到抽象类的子类

发布于 2024-12-28 02:26:59 字数 1074 浏览 1 评论 0原文

我有以下域模型要使用 ClassMaps/SubclassMaps

A - HasMany -> 进行映射B(A 应该是表)

B 在代码中是抽象的(并且不应该是表)

C、D、E 继承 B 并且都有非常不同的属性。它们中的每一个都应该是一个带有 A 外键的表。

我得到了我想要的表,但我不知道如何从具有一个属性 IList 的实体 A 映射 HasMany 。 SomeProperty

我想在 ClassMap 中定义 SomeProperty 在 C、D、E 上级联

这显然不起作用,在 ClassMap< ;A>:

HasMany(x => xB).Cascade.All();

HasMany(x => xB).Cascade.All(); HasMany(x => xB

).Cascade.All();

因为我无法复制 B。

示例:

public class Person
{
   public virtual IList<Animal> Animals { get; set; }

  public void AddAnimal(Animal animal)
  {
     Animals.Add(animal);
  }
}

public abstract class Animal
{
//some common properties
}

public class Cat : Animal
{
//some cat properties
}
public class Horse : Animal
{
//some horse properties
}

在本例中,我希望 ClassMap 通过抽象类 Animal 映射到 Cat 和 Horse。

I have the following domain model to map using ClassMaps/SubclassMaps

A - HasMany -> B (A should be table)

B is abstract in code (and should not be a table)

C,D,E inherits B and all have very different properties. Each of them should be a table with foreign key to A.

I get the tables I want, but I cannot see how to map HasMany from entity A that has one property IList<B> SomeProperty

I would like to define in ClassMap<A> that SomeProperty cascades on C,D,E

This obviously doesn't work, in ClassMap<A>:

HasMany<C>(x => x.B).Cascade.All();

HasMany<D>(x => x.B).Cascade.All();

HasMany<E>(x => x.B).Cascade.All();

As I cannot duplicate B.

Example:

public class Person
{
   public virtual IList<Animal> Animals { get; set; }

  public void AddAnimal(Animal animal)
  {
     Animals.Add(animal);
  }
}

public abstract class Animal
{
//some common properties
}

public class Cat : Animal
{
//some cat properties
}
public class Horse : Animal
{
//some horse properties
}

In this case I would like ClassMap<Person> to map to Cat and Horse over the abstract class Animal.

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

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

发布评论

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

评论(1

梓梦 2025-01-04 02:26:59
Try...

public void AddAnimal(Animal animal)
{
    animal.Person = this; //Recomended 
    Animals.Add(animal);
}


public abstract class Animal
{
     public virtual Person Person {get;set;} //Recomended Addition
//some common properties
}
Try...

public void AddAnimal(Animal animal)
{
    animal.Person = this; //Recomended 
    Animals.Add(animal);
}


public abstract class Animal
{
     public virtual Person Person {get;set;} //Recomended Addition
//some common properties
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文