Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EF CF Linq到Sql,是否保存附加的子实体?(.NET 4)_Linq_Entity Framework_Linq To Sql_Ef Code First - Fatal编程技术网

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;