Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[AcceptVerbs(HttpVerbs.Post)]

        public ActionResult CreateUser( [Bind(Exclude="IdUtilisateur")Utilisateur UtilisateurCree)
        {
            if (!ModelState.IsValid)

                return View();

            _db.AddToUtilisateurs(UtilisateurCree);
            _db.SaveChanges();        
            return RedirectToAction("CreateUser");
        }

_db.SaveChanges();


it returns an exception when creating!!!!!!!!!!!!
Posted
Updated 28-Dec-12 2:56am
v2
Comments
Oleksandr Kulchytskyi 28-Dec-12 5:55am    
How do you think , how can we help you from that little description??
Maybe it would be more efficient to add some exception message ??
Please accomplish your question
loylmed 28-Dec-12 7:47am    
that has an exception whene i try to create an object the exception is here
_db.AddToUtilisateurs(UtilisateurCree);
loylmed 28-Dec-12 8:02am    
"cannot insert value for identity colum in table 'users' when IDENTITY_INSERT in set to off'
but i search about the IDENTITY_INSERT but i can't find it !!!!!!

<br />
SET IDENTITY_INSERT [users] ON


But it's not relevant for EF, as i see, you have used

"I get the following error: Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF."*

I think that the IDENTITY_INSERT is the Auto Increment functionality which is off. So, check the field PersonId in the database to see if it is an identity.

Besides, maybe this will fix your problem too.

C#
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
 
Share this answer
 
v4
The problem lies in the AddToUtilisateurs method, without this I cannot be 100% certain what is wrong.

Given the error message you describe somewhere along the line you are trying to an object's data where the record already exists. In your case to the object is being persisted in the users table. What is happening is the User object (UtilisateurCree?) already has an Id (probably the primary key on the table) set, whereas the database is expecting to generate it for you. If you are updating an existing user, you will need to get the user object from the data store, update the information and save, rather than trying to re-add it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900