0
В случае необходимости добавления новой строки для отношений 1-1, нужно ли указывать Add или Attach? И как мне это сделать, если мне нужно?Нужно ли указывать добавление или прикрепление
//one tblContent to one tblContentData
//updating the tblContentData corresponding to a particular id in tblContent
int id = 12345;
tblContent entity = db.tblContents.Where(con => con.id == id)
.FirstOrDefault();
if (entity == null)
throw new Exception("id was bad");
if (entity.tblContentData == null)
entity.tblContentData = new tblContentData();
//proceed with updating the foreign keyed table
Вы должны были бы добавить 'db.Entry (сущность) .State = EntityState.Added;' для новой строки , или Entity Framework позаботится об этом для меня? –
@ChristopherStevenson - для нового элемента вы должны использовать 'Add'. Когда CRUD => Create's = Add, Edit, я использую Entry. –