简体   繁体   中英

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM