[英]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.