Tacy Nathan
Tacy Nathan

Reputation: 475

Adding data to a dB using EF 6.x

So I have 2 entities corresponding to 2 tables in the dB. Name of the 2 tables are: Doc and Users

Each entity has some properties. The relation between Doc and Users is 0 or 1 to many. I am doing something as following:

Doc document = new Doc();
...
...
document .Users.Add(new User{Name = "Temp", Info = "Some info.."});
context.AddToDocuments(document);
context.SaveChanges();

However, only the entries in Doc table is added in the dB. The User table in the dB is empty. Any suggestions?

Upvotes: 1

Views: 28

Answers (1)

Luc
Luc

Reputation: 1491

You should add each entity:

Doc document = new Doc();
...
...
User usr = new User() {Name = "Temp", Info = "Some info.."};
context.Users.Add(usr);
document .Users.Add(usr );
context.AddToDocuments(document);
context.SaveChanges();

Upvotes: 1

Related Questions