我試圖刪除我的實體與它相關的實體,但實體框架不想這樣做。EF核心不刪除相關實體
下面是代碼:
var tr = _context.Trees
.Include(x => x.Translation)
.FirstOrDefault(x => x.Id == 2);
_context.Remove(tr);
_context.SaveChanges();
語境:
modelBuilder.Entity<Tree>().ToTable("h_tree");
modelBuilder.Entity<Tree>().HasOne(x => x.Translation);
樹類:
public class Tree
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Translation Translation { get; set; }
}
任何人有任何想法,爲什麼相關的實體不能被刪除?
翻譯類:
public class Translation
{
public long Id { get; set; }
public string Pl { get; set; }
public string En { get; set; }
public string De { get; set; }
public string Cz { get; set; }
public string It { get; set; }
public string Ru { get; set; }
public string Fr { get; set; }
public Translation()
{
}
}
在數據集中是否真的有id = 2的記錄? –
當然可以。這是EF日誌: SET NOCOUNT ON; DELETE FROM [cat]。[h_tree] WHERE [id] = @ p0; SELECT @@ ROWCOUNT; – bielu000