如何使用NHibernate我更新的主键如何使用、主键、NHibernate

2023-09-04 03:06:43 作者:空城守旧梦

的主键我的一个表是一个字符串。该字符串是code,我想更新,在一定的时间点。我怎样才能做到这一点在NHibernate的。 请注意,连接到这个专栏,我需要级联更新外键。

为了便于讨论,让我们假设我的映射如下

 公共类code
{
    公共虚拟字符串ID {获得;组; }
    公共虚拟字符串名称{;组; }

    公共类codeMAP:ClassMap< code取代;
    {
        公共codeMAP()
        {
            表(业务code);
            ID(X => x.Id,ID);
            地图(X => x.Name,姓名)可空()。
        }

    }
}

公共类数据
{
    公共虚拟INT键{获得;组; }
    公共虚拟code数据code {获得;组; }
    公共虚拟字符串说明{获得;组; }
    公共类数据映射:ClassMap<数据>
    {
        公开数据映射()
        {
            表(数据);
            ID(X => x.Key,钥匙);
            地图(X => x.Desc).COLUMN(降序);
            参考文献(X => X code,商业code)。可空()Cascade.SaveUpdate();
        }
    }
}
 

表结构

 商业code表
ID(数据类型为nvarchar(100))-primarykey
名字(为nvarchar(1024))



数据表
ID(INT) - 自动生成主键
商业code(为nvarchar(100)) - 外键业务$ C C标识$
说明(为nvarchar(1024))
 

这里的code,我用它来更新我的目标

 使用(VAR反= Session.BeginTransaction())
            {
                VAR modifiedSource = Session.Load< code取代;(ID);
                modifiedSource.Id =NEWID;
                modifiedSource.Name =新名字;
                Session.Update(modifiedSource);
                trans.Commit();
            }
 
excel中每次都提示我更新源,编辑链接 请问我该如何操作呢

解决方案

尝试'分配' id生成:

 <类名=数据集表=数据集>
    < ID名称=ID列=id类型=字符串>
        <生成器类=分配/>
    < / ID>
    ...
< /类>
 

  ID(X => x.Id).GeneratedBy.Assigned();
 

从NHibernate的文档

  

5.1.4.7。分配的标识

     

如果你需要应用程序分配一个标示符(而非   有NHibernate的生成它们),你可以使用assigned生成器。   这种特殊的生成器会使用已经分配的标识值   对象的标识属性。要非常小心,当使用这种   功能几乎总是分配具有商业含义键(一个可怕   设计决定)。

     

由于其固有的性质,即使用这种生成不能实体   通过的ISession的saveOrUpdate()方法的方法保存。相反,你必须   明确指定以NHibernate的,如果该对象应保存或   通过调用在保存()或update()的方法更新   ISession的。

The primary key for one of my tables is a string. THe string is a code which i would like to update at some point of time. How can I do this in nhibernate. Please note there is a foreign key connected to this column which I need to cascade updates to.

For the sake of discussion let us assume my mapping is as below

public class Code
{
    public virtual string Id { get; set; }
    public virtual string Name { get; set; }

    public class CodeMap : ClassMap<Code>
    {
        public CodeMap()
        {
            Table("BusinessCode");
            Id(x => x.Id, "Id");
            Map(x => x.Name, "Name").Nullable();
        }

    }
}    

public class Data
{
    public virtual int Key { get; set; }
    public virtual Code DataCode{ get; set; }
    public virtual string Desc { get; set; }
    public class DataMap : ClassMap<Data>
    {
        public DataMap()
        {
            Table("Data");
            Id(x=>x.Key,"Key");
            Map(x => x.Desc).Column("desc");
            References(x => x.Code, "BusinessCode").Nullable().Cascade.SaveUpdate();
        }
    }
}

Table structure

BusinessCode Table
Id(nvarchar(100)) -primarykey
Name(nvarchar(1024))



Data Table
id(int) - auto generated primary key
businesscode(nvarchar(100)) - foreign key to BusinessCode Id
desc(nvarchar(1024))

here's the code which i use to update my object

using (var trans = Session.BeginTransaction())
            {
                var modifiedSource = Session.Load<Code>(id);
                modifiedSource.Id = "newId";
                modifiedSource.Name = "new name";
                Session.Update(modifiedSource);
                trans.Commit();
            }

解决方案

Try 'assigned' id generator:

<class name="Dataset" table="Dataset" >
    <id name="id" column="id" type="String">
        <generator class="assigned" />
    </id>
    ...
</class>

Or

Id(x => x.Id).GeneratedBy.Assigned();

From NHibernate doc

5.1.4.7. Assigned Identifiers

If you want the application to assign identifiers (as opposed to having NHibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

Due to its inherent nature, entities that use this generator cannot be saved via the ISession's SaveOrUpdate() method. Instead you have to explicitly specify to NHibernate if the object should be saved or updated by calling either the Save() or Update() method of the ISession.