Object structure adds / removes objects to many

For example, I have people, locations, and a personal table to link them. I also have a role and a personal table.

 Tables:

Person (personid, name)

Personlocation (personid, locationid)

Location (locationid, description)

Personrole (personid, roleid)

Role (roleid, description)

EF will provide me with faces, roles, and location objects.

Since EF will NOT generate the personlocation and personrole types, they cannot be used in the request.

QUESTION: how can I add a Person object and return a personid and then add this 3-role identifier to the Personrole table / association?

eg.

Person p = new Person();
p.name = "John"
......
entity.AddToPersons(p);
for(var roleid in Roleid)
entity.AddtoRoles(roleid)?
+3
source share
1 answer

EF , , FK, - . , " " .

: Person personid, 3 / Personrole?

"" "". "".

:

// Create new Person
Person p = new Person();
p.Name = "John";

// Create new Role
Role r = new Role();
r.Description = "Administrator";

// Add new Role to this new Person
p.Roles.Add(r);

// Add Person to context (no need to add Role)
ctx.AddToPersons(p);

// Save Changes
ctx.SaveChanges();

Entity Framework . , , , , .

.

EDIT - :

, , (, Roleid = 1, description = "Editor" )

.

:

Role r = ctx.Roles.SingleOrDefault(x => x.RoleId == 1 && x.Description == "Editor");
p.Roles.Add(r);
+2

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


All Articles