Click here to Skip to main content
13,199,026 members (39,414 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
[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 27-Dec-12 23:50pm
loylmed374
Updated 28-Dec-12 2:56am
Keith Barrow159.1K
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 !!!!!!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<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.

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100