sql server - Using NHibernate mapping by code: Cannot insert explicit value for identity column in table 'DietUser' when IDENTITY_INSERT is set to OFF -


I took some time to find an answer for this, thought that I shared love.


When I am unable to save an entity using the new mapping of NHibernate by code with SQL Server. When saving a system to a system Data.SqlClient.SqlException is given with the following message (decreased table name):

"IDENTITY_INSERT When the table 'DietUser' can not insert explicit values ​​for the identity column in the table Is set. "

My table contains an identity id and unit & amp; Mapping looks like this:

  public class user {public virtual id id (get; set;} public virtual string {get; set;} public virtual string user name {get; set; } Public Virtual String Password {get; set;} Public Virtual Small Daily Points {get} Set;}} Public Class Usermapping: Classmapping & lt; User & gt; {Public Usermapping () {id (x = & gt; x. Id;) Property (X = & gt; x.Name); property (x => x.Username); property (x => x password); property (x => x.ailyPoints);}} < / Code>  

I know how I am using this XML mapping, but I would probably like to use embedded mapping by code.

After finding some excavation in the forum, I got lucky and found how to map it. This is great if you use a GUID or something like this for ID Going on What if you're going to have seen a group would like to use an identification ID (other), you must specify that you use what generators. Here's my updated mapping that works:

  Public class usermapping: classmapping & lt; Users & gt; {Public usermapping () {id (x = & gt; x.id, map = & gt; map. Generator (generator id)); Property (x = & gt; x.Name); Property (x = & gt; x.Username); Property (x = & gt; x password); Property (x = & gt; x.DailyPoints); }}    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -