Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6379333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:07:29+00:00 2026-05-25T02:07:29+00:00

The primary key for one of my tables is a string. THe string is

  • 0

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();
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:07:30+00:00Added an answer on May 25, 2026 at 2:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update one value of a compound primary key from within the
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I have a database table and one of the fields (not the primary key)
In the following one to many CREATE TABLE source(id int, name varchar(10), PRIMARY KEY(id));
I need to have one column as the primary key and another to auto
We use GUIDs for primary key, which you know is clustered by default. When
this is not a primary key, but merely used as a marker in existing
We have a composite primary key for the site table defined below. Functionally, this
I have added a record to my table which auto-increments the primary key. I
I have a table structure like below : categoryID bigint , primary key ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.