2

I'm pretty sure this is a simple configuration error but my lack of experience is prohibiting me from fixing it. Basically I want to add an EMPLOYEE to a table using entity frame work.

Here is my method:

 public void createEmployee(CreateEmployeeModel model) { EMPLOYEE emp = new EMPLOYEE() { emp_name = model.Name, emp_email = model.Email, emp_cell_phone = model.CellPhone, emp_adr = model.Address }; _db.AddToEMPLOYEES(emp); _db.SaveChanges(); } 

I get the following error on the _db.SaveChanges(); call: Cannot insert duplicate key row in object 'dbo.EMPLOYEES' with unique index 'R18_SDE_ROWID_UK'. The duplicate key value is (0).

Here is the emp_id column properties for the EMPLOYEE Entity:

StoreGeneratedPattern : Identity Concurrency Mode : None Default Value : (None) Entity Key : True Name: emp_id Nullable: False Type : Int32 

Here is the emp_id column properties for the EMPLOYEES Table on SQL server:

enter image description here

I'm not sure why the primary key is always being generated as a 0 when there is already 4 employees in the table. (0,1,2,3).

3
  • Is this Code first or edmx/DB first? What does your model look like? Commented Apr 19, 2012 at 20:10
  • I really think it is a configuration problem because I had to change the properties on emp_id to make it auto increment. There is a good chance that I did that wrong. Commented Apr 19, 2012 at 20:15
  • Yes, ill add a screen shot of the properties window. Commented Apr 19, 2012 at 20:18

2 Answers 2

2

If you had to manually change the properties of the model, chances are you did not set the column to be an Identity column in the database itself.

If you have set the database to be an identity column, then delete the entity and re-add it.

Sign up to request clarification or add additional context in comments.

3 Comments

I did this and it changed one of my associations from a (0..1) to (0..1) to a many to (0..1). I tried adjusting the association back to the original and it throws this error Error 25 Error 113: Multiplicity is not valid in Role 'EMPLOYEE' in relationship 'FK_aspnet_UsersEMPLOYEE'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
@ZeroDivide - that means your database model is different from what you have in your data model. You can't just change your code model to whatever you want, it must correspond to the actual data model. In your case, you either changed an association manually in the first place (something you can't do) or you you changed your database since your model was generated. In particular, you can't have a 1:1 (or 0..1 to 0..1) unless both tables have the same primary key. Also, only one of them can be an identity, because they have to share the same key value.
Yeah, I had to change both the data model and the entity model a while back to add this association/relation. It looks like something has been screwed up with all the changes being made to the database. I had to leave the office but it looks like your solution was the one.
0

I think you may need to do something like _db.EMPLOYEES.CreateObject() to get a new instance of EMPLOYEE set up properly.

var emp = _db.EMPLOYEES.CreateObject(); //set values _db.EMPLOYEES.AddObject(emp); _db.SaveChanges(); 

1 Comment

No, you don't need to do that. That is only needed if you want to generate change tracking proxies in certain situations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.