C# EF核心不删除相关实体

C# EF核心不删除相关实体,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我试图删除与it相关的实体,但实体框架不想这样做 代码如下: var tr = _context.Trees .Include(x => x.Translation) .FirstOrDefault(x => x.Id == 2); _context.Remove(tr); _context.SaveChanges(); 背景: modelBuilder.Entity<Tre

我试图删除与it相关的实体,但实体框架不想这样做

代码如下:

        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 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()
    {

    }
}

你似乎没有说出这是一对一还是一对多的关系

.HasOne需要与.with*方法配对。要么用一个,要么用多个

您的翻译类似乎缺少外键


添加一个名为TreeId的属性,并在.WithOne调用中使用它。

我尝试了以下语法:modelBuilder.Entity.HasOnex=>x.Translation.WithOne.OnDeleteBehavior.Cascade;但它不起作用。我有以下错误:无法确定在“Tree.Translation”和“Translation”之间检测到的一对一关系的子/依赖方。若要标识关系的子/依赖方,请配置外键属性。@bielu000那么翻译中的外键字段是什么?为什么不将它放在.WithOne方法中?我需要用WithOne方法输入什么?此语法不正确:modelBuilder.Entity.HasOnex=>x.Translation.WithOnex=>x.Id我有一个错误:modelBuilder.Entity.HasOnex=>x.Translation.WithOnex=>x。Id@bielu000我不知道,因为我不知道你们的翻译课是什么样子。您的外键是什么?数据集中是否确实存在id==2的记录?当然有。这是一个EF日志:设置NOCOUNT ON;从[cat].[h_树]中删除,其中[id]=@p0;选择@@ROWCOUNT;
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()
    {

    }
}