开发者

NHibernate: How to add a List<Product> that i created manually to Nhibernate?

开发者 https://www.devze.com 2023-01-24 07:00 出处:网络
I have created a list of products - i.e. List manually in code. Is it possible with nhibernate to send along a List of Product and have it automatically add the details.

I have created a list of products - i.e. List manually in code.

Is it possible with nhibernate to send along a List of Product and have it automatically add the details.

The property on the product class is an automatic increment ID o开发者_如何转开发n the database.

I have it defined like so

  public virtual int Id { get; set; }

But of course this is a automatic incremental field in the database so what should i set it to on the object (standard c# class) for it to import to the database.

If this is possible.

In my mappings i have it set like so

<class name="Product">
  <id name="Id" column="ProductID">
    <generator class="assigned" />
  </id>
<property name="Name" />
<more property names are here.............>

I think it must be possible but i am having a problem understanding how i can send a collection of my Product model (List) to nhibernate to be inserted automatically

Any help really appreciated

EDIT

I would like to clarify what i am doing.. I have the product class which is basically the entity. I have created a number of products which are in a LIST like so List

hence with the collection of products i would like to BATCH insert them into the database.

And clarify that the database's ProductID field is auto generated, hence what do i set the Class' ID to as technically its going to be inserted and the database will take care of this field.

here is my property which represents the ID in the database

  public virtual int Id { get; set; }

Maybe i should set it to a nullable INT and then i can set it to NULL????? Although i am guessing

I hope i have explained myself a little better.


Updated

It is helpful to think of NHibernate as an entity persister at the object level. It is not a strong bulk data loader / manipulator (I'm not aware of an ORM that is).

So given your example the correct thing to do is to call Session.SaveUpdate for each item in your list. So assuming you have access to your NHibernate Session object you simply call:

foreach(Product p in ProductList){
  Session.SaveOrUpdate(p);
}

Personally I usually wrap this logic into the repository (I usually use the repository pattern) an example would be something like this:

public void SaveOrUpdateAll(IEnumerable<Product> products){
  foreach(Product p in products){
    Session.SaveOrUpdate(p);
  }
}

As to you second question: What to do with the Id? This is simple, leave the Id as is. The int type cannot be null so this is initialized to 0. This is enough to tell NHibernate that this entity is not yet persisted in the database and it will be added (an added bonus your product will get its Id set to the auto-generated database key after the SaveOrUpdate method is called.

Of interest NHibernate makes the determination when using the SaveOrUpdate method to create a new record in the database or update an existing record depending on whether this number is > 0 (i.e. assigned).

This is dependent on your mapping being correct so your generator has to be as follows:

<generator class="identity" /> 
0

精彩评论

暂无评论...
验证码 换一张
取 消