Fluent nHibernate - 使用复合键映射多对多
我正在开发一个具有如下表结构的应用程序: (* 表示键)
产品:
*产品ID
*品牌
产品名称
类别
*类别ID
类别名称
产品类别
*类别ID
*ProductID
产品具有 ProductID 和 ProductID 的复合 ID。品牌
类别如下:
public class Product
{
public int ProductID { get; set; }
public string Brand{ get; set; }
public string ProductName { get; set; }
public IEnumerable<Category> { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
在我的产品映射中,我有
HasManyToMany(x => x.Categories).Table("ProductCategories")
.ParentKeyColumn(NameOf<Product>.Property(p => p.ProductID))
.ChildKeyColumn(NameOf<Category>.Property(p => p.CategoryID))
.Cascade.All();
所以,基本上,我试图根据 ProductCategories 表中的 ProductID 选择类别... 这可能吗?
然而- 我收到如下错误:
必须具有与引用的主键相同的列数(产品 [ProductID、品牌])
I'm developing an application that has a table structure as follows:
(* denotes key)
Product:
*ProductID
*Brand
ProductName
Categories
*CategoryID
CategoryName
ProductCategories
*CategoryID
*ProductID
Product has a composite ID of ProductID & Brand
Classes are as follows:
public class Product
{
public int ProductID { get; set; }
public string Brand{ get; set; }
public string ProductName { get; set; }
public IEnumerable<Category> { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
In my mapping for Product, I have
HasManyToMany(x => x.Categories).Table("ProductCategories")
.ParentKeyColumn(NameOf<Product>.Property(p => p.ProductID))
.ChildKeyColumn(NameOf<Category>.Property(p => p.CategoryID))
.Cascade.All();
So, basically, I'm trying to have the Categories selected based on the ProductID in ProductCategories table...
Is that possible?
However-
I'm getting an error like:
must have same number of columns as the referenced primary key (Product [ProductID, Brand])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 Product 具有 ProductID 和 ProductID 的复合 ID; Brand 那么在 ProductCategories 表中必须是 ProductID、Brand 和 CategoryID 的复合键。否则你会得到一个错误:
并且映射应该像-
If Product has a composite ID of ProductID & Brand then In ProductCategories table must be Composite key of ProductID, Brand and CategoryID. Otherwise u will get a error:
And mapping Should be like-
你的代码几乎是正确的。问题是 .ParentKeyColumn(...) 和 .ChildKeyColumn(...) 需要 Db 列名称。检查字符串 NameOf.Property(p => p.ProductID) 是否对应于您的列名称。还可以尝试明确指定类别的类型。
希望这就是问题所在。我在代码中使用完全相同的映射。
Your code is almost correct. The problem is that .ParentKeyColumn(...) and .ChildKeyColumn(...) expects Db column name. Check if the string NameOf.Property(p => p.ProductID) corresponds to your column name. Also try Specifying explicitly the Type of the categories.
Hope this is the problem. I am using exactly the same mapping in my code.
好吧,在这种情况下你应该使用这个:
Ok well in that case you should use this: