简体   繁体   English

C#Nhibernate-将新项目添加到多对多关系

[英]C# Nhibernate - Adding new item to many-to-many relationship

So i have the two tables (Product and Supplier) connected through a many-to-many relationship. 因此,我通过多对多关系连接了两个表(产品和供应商)。 They're coded as follows: 它们的编码如下:

class Product
{
    public virtual uint id { get; set; }
    public virtual string name { get; set; }
    public virtual float stock { get; set; }
    public virtual float value { get; set; }
    public virtual Category category { get; set; }

    public virtual ISet suppliers { get; set; }
    public virtual ISet saleItems { get; set; }
    public virtual ISet stockInlets { get; set; }
}

- -

class Supplier
{
    public virtual uint id { get; set; }
    public virtual string name { get; set; }
    public virtual string CNPJ { get; set; }
    public virtual string addressLine1 { get; set; }
    public virtual string addressLine2 { get; set; }
    public virtual string telephone1 { get; set; }
    public virtual string telephone2 { get; set; }

    public virtual ISet products { get; set; }
    public virtual ISet stockInlets { get; set; }
}

And this is how I mapped their relationship: 这就是我映射他们的关系的方式:

<set name="suppliers" table="product_x_supplier" fetch="join" lazy="false">
  <key column="product_id" />
  <many-to-many class="Supplier" column="supplier_id" />
</set>

- -

<set name="products" table="product_x_supplier" fetch="join" lazy="false">
  <key column="supplier_id" />
  <many-to-many class="Product" column="product_id" />
</set>

Now, I'm having a problem when inserting a new Product and trying to link the correspondent Suppliers to it. 现在,在插入新产品并尝试将相应的供应商链接到该产品时,我遇到了问题。 I did manage to Update an already existing one, but not to create a new one. 我确实设法更新了一个现有的,但是没有创建一个新的。 Here's the (simplified) code I'm trying to use: 这是我尝试使用的(简化)代码:

Product p = new Product()
{
    name = "Test product",
    stock = 0,
    value = 2,
    category = (Category)cats[0]
};

for (int i = 0; i < suppliers.Length; i++)
{
    p.suppliers.Add(suppliers[i]);
}

repository.Add(p);

It tells me Object reference not set to an instance of an object. 它告诉我Object reference not set to an instance of an object. . I tried to instantiate the object, doing something like p.suppliers = new IList<Supplier>(); 我试图实例化该对象,执行类似p.suppliers = new IList<Supplier>(); but then it complained it Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList<TCC.Hibernate.Domain.Classes.Supplier>' . 但随后它抱怨它Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList<TCC.Hibernate.Domain.Classes.Supplier>'

Any help is appreciated. 任何帮助表示赞赏。

Use... 采用...

p.suppliers = new List<Supplier>();

Your list needs to be initialized prior to adding items to it. 您的列表需要先初始化,然后再添加项目。 IList is an interface, not a class, you need to use List , the default implementation of IList . IList是接口,而不是类,您需要使用ListIList的默认实现)。

As an aside, it seems you may not be that familiar with the C# language or the built-in .NET libraries. 顺便说一句,您似乎可能并不熟悉C#语言或内置的.NET库。 I would suggest becoming more familiar with basic C# development before playing with third-party libraries like Hibernate. 我建议在使用Hibernate之类的第三方库之前,先熟悉基本的C#开发。 Hibernate is an especially complex library that will be the cause of many headaches for you if you have troubles diagnosing even simple errors like these. Hibernate是一个特别复杂的库,如果您在诊断甚至这些简单错误时都遇到麻烦,将使您头痛不已。

Good luck! 祝好运!

UPDATE: 更新:

Sorry, I didn't notice that your field types were ISet , not IList . 抱歉,我没有注意到您的字段类型是ISet ,而不是IList You will have to use an ISet implementation like HashSet or SortedSet as Sly has demonstrated in their answer. 正如Sly在他们的答案中所演示的那样,您将必须使用ISet实现(例如HashSetSortedSet

You can browse the MSDN to learn more about these collection types... 您可以浏览MSDN,以了解有关这些集合类型的更多信息...

http://msdn.microsoft.com/en-us/library/0sbxh9x2 http://msdn.microsoft.com/en-us/library/0sbxh9x2

Use 采用

 class Supplier
 {
      public Supplier()
      {
          this.suppliers = new HashedSet<Supplier>();
      }

      public virtual ICollection<Product> products { get; private set; }
 }

This way you will ensure that collection is always initialized and nobody can replace it. 这样,您将确保集合始终被初始化,并且没有人可以替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM