使用 confORM 映射 NHibernate

发布于 2024-12-13 02:22:02 字数 759 浏览 1 评论 0原文

我有 2 个类(为了简洁而总结):

public class Product : Entity<Guid>
{      
    ...    
    public virtual IList<Ingredient> Ingredients { get; set; }          
    public Product(){Ingredients = new List<Ingredient>();}    
}

它们

public partial class Ingredient : Entity<int>
{
    ...
    public virtual IList<Product> Products { get; set; }
    public Ingredient(){Products = new List<Product>();}
}

具有多对多关系,我想做:

  • 如果我删除一种成分,则该产品不会被删除,而只会删除他列表中的成分。
  • 如果我删除一种产品,其成分全部不会被删除。

我做了这张地图,但我无法让它工作。

orm.ManyToMany<Product, Ingredient>();
orm.Cascade<Product, Ingredient>(CascadeOn.DeleteOrphans);

I have 2 classes (summarized for brevity) :

public class Product : Entity<Guid>
{      
    ...    
    public virtual IList<Ingredient> Ingredients { get; set; }          
    public Product(){Ingredients = new List<Ingredient>();}    
}

and

public partial class Ingredient : Entity<int>
{
    ...
    public virtual IList<Product> Products { get; set; }
    public Ingredient(){Products = new List<Product>();}
}

They have a ManyToMany relationship, and I want to do:

  • if I delete one ingredient, the product is not removed but only the ingredient for his list.
  • if I delete one product, the ingredients are all without being deleted.

I did this map, but I can't get this to work.

orm.ManyToMany<Product, Ingredient>();
orm.Cascade<Product, Ingredient>(CascadeOn.DeleteOrphans);

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

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

发布评论

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

评论(1

往日 2024-12-20 02:22:02

终于,我明白了。
这是我可以解决这个问题的方法,以防帮助别人更多:

        orm = new ObjectRelationalMapper();
        mapper = new Mapper(orm);
        //...

        mapper.Class<Ingredient>(c =>
        {
           /* ...[MAP OTHERS PROPERTY]...*/
           // Many to many relationship in One side
            c.Bag(p => p.Products, pm => pm.Inverse(false), rel => rel.ManyToMany());
        });

       // Many to many relationship in other side
       orm.ManyToMany<Product, Ingredient>();

Finally, I got it.
This is the way I could resolve this, in case to help someone more:

        orm = new ObjectRelationalMapper();
        mapper = new Mapper(orm);
        //...

        mapper.Class<Ingredient>(c =>
        {
           /* ...[MAP OTHERS PROPERTY]...*/
           // Many to many relationship in One side
            c.Bag(p => p.Products, pm => pm.Inverse(false), rel => rel.ManyToMany());
        });

       // Many to many relationship in other side
       orm.ManyToMany<Product, Ingredient>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文