0

i search but i dont find answer. i use ADO.NET Entity Framework,sql server. when i Update Wizard will not add table. tables are:

users{UserId:primaryKey,Name,StateID:ForeignKey to states Table}

states{stateId:primaryKey,Name}

user_state{UserId:primaryKey,ForeignKey to users table, stateId:primaryKey,ForeignKey to states table }

i add Users,sates Tables to model successfull. but when i update model and add user_state table to model,dont add and add a new relation between state and user tables.why?what is wrong?

thank you in advanced.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • There's nothing wrong. The wizard is smart enough to realize this is a many-to-many relation table, not a real entity, so it creates a many-to-many relation between the two *entities*. – Panagiotis Kanavos Oct 14 '15 at 09:53
  • ok,but how to insert row in user_state?there is a class with this name? – user122121212 Oct 14 '15 at 10:08
  • I think you need to check a tutorial on EF and ORMs. The classes are *not* tables. Instead of adding rows or changing foreign keys, you should be setting one object's property to the other, eg `someUser.State=someState`. Or in your case, since you have more than one state per user, add the state to the `States` property, eg `someUser.States.Add(someState);`. – Panagiotis Kanavos Oct 14 '15 at 10:15
  • [This SO question](http://stackoverflow.com/questions/8869632/entity-framework-update-many-to-many-relationship-virtual-or-not) shows how you can update a many-to-many relation. – Panagiotis Kanavos Oct 14 '15 at 10:18
  • thanks alot,that very good! – user122121212 Oct 16 '15 at 10:03

0 Answers0