Entity Framework - add only foreign key values ​​to the union

OK, I have 3 tables, name them:

Person

  • Personid
  • Name

Score

  • StoreID
  • Name

Personstore

  • Personid
  • StoreID

Now I have a form that allows you to add stores to a person. However, I am returning the store identifier from the form. I really don't want to make a request to get the store object from the Entity Framework. I just want to add to the table using the StoreID object and the Person object that I have.

+3
source share
1 answer

By default, in EF, this join table will not be displayed as an entity; instead, you will get many, many relationships that will be displayed as two navigation properties

.

Person.Stores
Store.People

- , - - .

var person = // you already have the person
var store = new Store{StoreID = 5} // you know the storeID

ctx.AttachTo("Stores", store);
ctx.AttachTo("People", person); // assuming the person isn't already attached
person.Stores.Add(store);
ctx.SaveChanges();

- , , ,

Stub, . , .

OP:

EF4, ( 13 ).

var person = // you already have the person
var store = new Store{StoreID = 5} // you know the storeID

ctx.Stores.Attach(store);
person.Stores.Add(store);
ctx.SaveChanges();
+5

Source: https://habr.com/ru/post/1766231/


All Articles