Fluent nHibernate - 使用复合键映射多对多

发布于 2024-12-27 09:10:40 字数 1018 浏览 2 评论 0原文

我正在开发一个具有如下表结构的应用程序: (* 表示键)

产品:
*产品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 技术交流群。

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

发布评论

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

评论(3

锦欢 2025-01-03 09:10:40

如果 Product 具有 ProductID 和 ProductID 的复合 ID; Brand 那么在 ProductCategories 表中必须是 ProductID、Brand 和 CategoryID 的复合键。否则你会得到一个错误:

并且映射应该像-

HasManyToMany<Category>(x => x.Categories)
    .Table("ProductCategories") 
    .ParentKeyColumns.Add("product_id","Brand") 
    .ChildKeyColumn("category_id") 

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-

HasManyToMany<Category>(x => x.Categories)
    .Table("ProductCategories") 
    .ParentKeyColumns.Add("product_id","Brand") 
    .ChildKeyColumn("category_id") 
ぃ双果 2025-01-03 09:10:40

你的代码几乎是正确的。问题是 .ParentKeyColumn(...) 和 .ChildKeyColumn(...) 需要 Db 列名称。检查字符串 NameOf.Property(p => p.ProductID) 是否对应于您的列名称。还可以尝试明确指定类别的类型。

HasManyToMany<Category>(x => x.Categories)
    .Table("ProductCategories") //  DB table name
    .ParentKeyColumn("product_id") // column name
    .ChildKeyColumn("category_id") // column name

希望这就是问题所在。我在代码中使用完全相同的映射。

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.

HasManyToMany<Category>(x => x.Categories)
    .Table("ProductCategories") //  DB table name
    .ParentKeyColumn("product_id") // column name
    .ChildKeyColumn("category_id") // column name

Hope this is the problem. I am using exactly the same mapping in my code.

南风几经秋 2025-01-03 09:10:40

好吧,在这种情况下你应该使用这个:

HasManyToMany<Category>(x => x.Categories)
    .Table("ProductCategories") 
    .ParentKeyColumn("ProductID") 
    .ChildKeyColumn("CategoryID")

Ok well in that case you should use this:

HasManyToMany<Category>(x => x.Categories)
    .Table("ProductCategories") 
    .ParentKeyColumn("ProductID") 
    .ChildKeyColumn("CategoryID")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文