EF CF Linq到Sql,是否保存附加的子实体?(.NET 4)
我得到的代码只更新对象的特定字段EF CF Linq到Sql,是否保存附加的子实体?(.NET 4),linq,entity-framework,linq-to-sql,ef-code-first,Linq,Entity Framework,Linq To Sql,Ef Code First,我得到的代码只更新对象的特定字段 var myEntity = new DBEntity { Id = 1, Field1 = true, Field2 = "Test" }; context.DBEntities.Attach(myEntity); var entry = context.Entry(myEntity); entry.Property(x => x.Field1).IsModified = true; entry.Property(x => x.Field2).Is
var myEntity = new DBEntity { Id = 1, Field1 = true, Field2 = "Test" };
context.DBEntities.Attach(myEntity);
var entry = context.Entry(myEntity);
entry.Property(x => x.Field1).IsModified = true;
entry.Property(x => x.Field2).IsModified = true;
context.SaveChanges();
这工作做得很好。现在我想在我的实体对象中添加一个日志条目,每次有人进行更新时,我都想在我的日志中跟踪它
var myEntity = new DBEntity { Id = 1, Field1 = true, Field2 = "Test" };
var logEntry = new DBEntityLog { LogText = "Update to field1 and field2" };
myEntity.Logs.Add(logEntry);
context.DBEntities.Attach(myEntity);
var entry = context.Entry(myEntity);
entry.Property(x => x.Field1).IsModified = true;
entry.Property(x => x.Field2).IsModified = true;
// Property does not work on collection objects
context.SaveChanges();
现在如何将日志条目保存到数据库中?现在它仍然只会更新字段。通过获取日志条目并将EntityState设置为added解决了这个问题
var entryLog = cibtext,Entry(logEntry);
entryLog.State = EntityState.Added;