如何在一个流畅的nhibernate中将实体映射到entitycollection字典(How to map an entity to entitycollection dictionary in f

编程入门 行业动态 更新时间:2024-10-24 08:20:43
如何在一个流畅的nhibernate中将实体映射到entitycollection字典(How to map an entity to entitycollection dictionary in fluent nhibernate)

我有这些类(从他们删除冗余信息后):

public class Category : BaseEntity { public virtual Category ContainingCategory { get; set; } public virtual IList<CategoryProperty> Properties { get; set; } } public class CategoryProperty : BaseEntity { public virtual string Name { get; set; } public virtual IList<CategoryPropertyValue> Values { get; set; } } public class CategoryPropertyValue : BaseEntity { public virtual string Name { get; set; } } public class CategoryPropertyValueCollection : BaseEntity { public virtual IList<CategoryPropertyValue> Values { get; set; } } public class Product : BaseEntity { public virtual string Name { get; set; } public virtual Category ContainingCategory { get; set; } public virtual IDictionary<CategoryProperty, CategoryPropertyValueCollection> Properties { get; set; } }

这些是我的映射(再次删除冗余信息后):

public class CategoryMap : BaseMap<Category> { public CategoryMap() { Map(x => x.Name); HasMany(x => x.Properties); } } public class CategoryPropertyMap : BaseMap<CategoryProperty> { public CategoryPropertyMap() { Map(x => x.Name); HasMany(x => x.Values); } } public class CategoryPropertyValueMap : BaseMap<CategoryPropertyValue> { public CategoryPropertyValueMap() { Map(x => x.Name); } } public class CategoryPropertyValueCollectionMap : BaseMap<CategoryPropertyValueCollection> { public CategoryPropertyValueCollectionMap() { HasMany(x => x.Values).Cascade.None(); } } public class ProductMap : BaseMap<Product> { public ProductMap() { Map(x => x.Name); HasMany(x => x.Properties).AsEntityMap(); } }

我的映射出了问题,很容易看出我想要实现的目标(ebay将成为该逻辑的一个很好的例子 - 来自具有不同属性和可能值的类别的产品可以过滤)。 这是我得到的表(不好):

create table Categories ( Id INT IDENTITY NOT NULL, Name NVARCHAR(255) null, ContainingCategoryId INT null, primary key (Id) ) create table CategoryProperties ( Id INT IDENTITY NOT NULL, Name NVARCHAR(255) null, CategoryId INT null, primary key (Id) ) create table CategoryPropertyValueCollections ( Id INT IDENTITY NOT NULL, ProductId INT null, CategoryProperty_id INT null, primary key (Id) ) create table CategoryPropertyValues ( Id INT IDENTITY NOT NULL, Name NVARCHAR(255) null, CategoryPropertyId INT null, CategoryPropertyValueCollectionId INT null, OrderItemId INT null, CategoryProperty_id INT null, primary key (Id) )

为什么' CategoryPropertyValues '是使用' CategoryPropertyValueCollectionId '列创建的? 我在映射中做错了什么?

I have this classes (after removing redundant information from them):

public class Category : BaseEntity { public virtual Category ContainingCategory { get; set; } public virtual IList<CategoryProperty> Properties { get; set; } } public class CategoryProperty : BaseEntity { public virtual string Name { get; set; } public virtual IList<CategoryPropertyValue> Values { get; set; } } public class CategoryPropertyValue : BaseEntity { public virtual string Name { get; set; } } public class CategoryPropertyValueCollection : BaseEntity { public virtual IList<CategoryPropertyValue> Values { get; set; } } public class Product : BaseEntity { public virtual string Name { get; set; } public virtual Category ContainingCategory { get; set; } public virtual IDictionary<CategoryProperty, CategoryPropertyValueCollection> Properties { get; set; } }

And those are my mapping (after removing redundant information again):

public class CategoryMap : BaseMap<Category> { public CategoryMap() { Map(x => x.Name); HasMany(x => x.Properties); } } public class CategoryPropertyMap : BaseMap<CategoryProperty> { public CategoryPropertyMap() { Map(x => x.Name); HasMany(x => x.Values); } } public class CategoryPropertyValueMap : BaseMap<CategoryPropertyValue> { public CategoryPropertyValueMap() { Map(x => x.Name); } } public class CategoryPropertyValueCollectionMap : BaseMap<CategoryPropertyValueCollection> { public CategoryPropertyValueCollectionMap() { HasMany(x => x.Values).Cascade.None(); } } public class ProductMap : BaseMap<Product> { public ProductMap() { Map(x => x.Name); HasMany(x => x.Properties).AsEntityMap(); } }

Something is wrong with my mappings, it's easy to see what I'm trying to accomplish (ebay will be a good example of that logic - products from categories that has properties that varies and possible values to filter by). This is that tables I'm getting (which aren't good):

create table Categories ( Id INT IDENTITY NOT NULL, Name NVARCHAR(255) null, ContainingCategoryId INT null, primary key (Id) ) create table CategoryProperties ( Id INT IDENTITY NOT NULL, Name NVARCHAR(255) null, CategoryId INT null, primary key (Id) ) create table CategoryPropertyValueCollections ( Id INT IDENTITY NOT NULL, ProductId INT null, CategoryProperty_id INT null, primary key (Id) ) create table CategoryPropertyValues ( Id INT IDENTITY NOT NULL, Name NVARCHAR(255) null, CategoryPropertyId INT null, CategoryPropertyValueCollectionId INT null, OrderItemId INT null, CategoryProperty_id INT null, primary key (Id) )

Why does 'CategoryPropertyValues' is created with a 'CategoryPropertyValueCollectionId' column? What am I doing wrong in the mapping?

最满意答案

发现我的问题是使用HasMany而不是HasManyToMany

Found out my problem was the usage of HasMany instead of HasManyToMany.

更多推荐

public,class,set,Properties,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-08-05 03:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   字典   实体   流畅   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!