[英]Entity Framework 4.4 SaveChanges Exclude Related Entity
我正在使用.NET 4.0的EntityFramework.dll 4.4版。 我有一个实体,其中包含对另一个实体的引用,如下所示:
[Table("Bar")]
public class Bar
{
public string Id { get; set; }
public Foo Foo { get; set; }
[ForeignKey("Foo")]
public string FooId {get; set; }
}
当我想向数据库中添加新的“ Bar”记录时,EntityFramework尝试还添加“ Foo”的实例,但是我不希望它这样做。 创建Bar时,是否有办法告诉EF忽略Foo实体? 我不想在Foo上设置[NotMapped],因为它确实需要映射-只是我不想保存它。 所以我希望以下工作:
public void CreateBar(Bar b)
{
_barContext.Bars.Add(b);
// This function doesn't exist, but I would like it to exist
_barContext.Exclude("Foo");
_barContext.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.