c# - Why does a newly created EF-entity throw an ID is null exception when trying to save? -


I am trying to incorporate the unit structure in VS2010, but there is a problem with the model generated from my database / graphical interface.

When I do:

  User = Dataset. UserAct.net Object (); User.Id = Guid.NewGuid (); Dataset.UserSet.AddObject (user); Dataset.SaveChanges (); "Value" can not be inserted into the column 'id', table 'BarSoc2.dbo.UserSet'; Column faucets do not allow. INSERT fails. \ R \ N statement has been aborted. "}  

The table looks like this:

  - Create table 'UserSet' table [dbo]. [UserSet] ([Id] is not a unique identifier, [name] nvarchar (maximum) No, zero, [user name] nvarchar (max) no, zero, [password] nvarchar (maximum) zero); Go - change the primary key on [UD] by changing the table to 'UserSet' [DBO]. [UserSet] ADD Contract [PK_UserSat] Primary Key Cluster ([ID] ASC); GO  

Am I incorrectly creating an object or doing something wrong?

You should not set the id property manually. When you save it should be set automatically I think you are using standard templates for unit framework, and not for self-tracking entities template or POCOs template. If so, something would be more appropriate with the following lines:

  user user = new user (); Dataset.AddToUsers (user); Dataset.SaveChanges ();  

Comments