NHibernate:如何添加列表我手动创建到 Nhibernate?

发布于 2024-10-01 12:58:06 字数 974 浏览 0 评论 0原文

我创建了一个产品列表 - 即在代码中手动列出。

nhibernate 是否可以发送产品列表并让它自动添加详细信息。

产品类的属性是数据库中的自动增量 ID。

我像这样定义了它

  public virtual int Id { get; set; }

,但是当然这是数据库中的自动增量字段,所以我应该在对象(标准 C# 类)上将其设置为什么才能将其导入数据库。

如果这可能的话。

在我的映射中,我将其设置为这样,

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

我认为它一定是可能的,但我在理解如何将我的产品模型(列表)的集合发送到 nhibernate 以自动插入时遇到问题

任何帮助非常感谢

编辑< /strong>

我想澄清一下我在做什么。我有产品类,它基本上是实体。我创建了许多产品,这些产品位于这样的列表中,

因此我希望将产品集合批量插入数据库中。

并澄清数据库的 ProductID 字段是自动生成的,因此我应该将类 ID 设置为什么,从技术上讲,它将被插入,并且数据库将处理该字段。

这是我的属性,它代表数据库中的 ID

  public virtual int Id { get; set; }

也许我应该将其设置为可为空的 INT,然后我可以将其设置为 NULL???虽然我猜测

我希望我能更好地解释自己。

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 on 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

§对你不离不弃 2024-10-08 12:58:06

已更新

将 NHibernate 视为对象级别的实体持久化器会很有帮助。它不是一个强大的批量数据加载器/操纵器(我不知道是 ORM)。

因此,根据您的示例,正​​确的做法是为列表中的每个项目调用 Session.SaveUpdate 。因此,假设您可以访问 NHibernate Session 对象,您只需调用: 就我

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

个人而言,我通常将此逻辑包装到存储库中(我通常使用存储库模式),示例如下:

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

至于第二个问题:如何处理ID?这很简单,保留 Id 不变即可。 int 类型不能为 null,因此它被初始化为 0。这足以告诉 NHibernate 该实体尚未持久化在数据库中并且将被添加(一个额外的好处是您的产品将其 Id 设置为自动生成的调用 SaveOrUpdate 方法后,

NHibernate 会根据该数字是否大于 0(即已分配)来确定何时使用 SaveOrUpdate 方法在数据库中创建新记录或更新现有记录

。你的映射是正确的,所以你的生成器必须如下:

<generator class="identity" /> 

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" /> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文