Click here to Skip to main content
11,417,219 members (57,673 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC4
[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 28-Dec-12 0:50am
loylmed374
Edited 28-Dec-12 3:56am
Keith Barrow153.3K
v2
Comments
Oleksandr Kulchytskyi at 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 at 28-Dec-12 7:47am
   
that has an exception whene i try to create an object the exception is here
_db.AddToUtilisateurs(UtilisateurCree);
loylmed at 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


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
0 Sergey Alexandrovich Kryukov 9,295
1 OriginalGriff 7,592
2 Maciej Los 3,718
3 Abhinav S 3,456
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web01 | 2.8.150427.4 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
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