繁体   English   中英

EF Core不保存ParentId

[英]EF Core not saving ParentId

我正在尝试构建“作业”对象的结构。 这些用来描述一个人需要做的整个过程。

例如。

如果该作业的描述为“煮咖啡”,则该作业将具有其他作业列表,这些作业一起完成后就可以制作咖啡。

一个示例结构可能是:

Make Coffee
  |- Boil Kettle
     |- Fill Kettle
     |- Turn on Kettle
  |- Get a cup
  |- Place instant coffee in the cup
  |- Pour boiling water in the cup
  |- Add milk to the cup

还应注意,每个Job都有一个版本号; 和VersionOfID。 这是在需要更改工作的情况下,只有一个版本是“ Live”,并且将被使用。

我将对象添加到EF Core DBContext中,在调用SaveChanges之前,我可以正确看到该结构。 但是,一旦关闭/重新加载上下文,作业将不再相互引用。 我可以在SQL Management Studio中看到所有对象的ParentID为null。

Job的类如下:

public class Job
{
    public int JobId { get; set; }

    [Required]
    public string Description { get; set; }

    public string Code { get; set; }

    public int Quantity { get; set; } = 1;

    public int Time { get; set; }

    public TimeUnit UnitOfTime { get; set; }

    [ForeignKey("VersionOf")]
    public int? VersionOfId { get; set; }
    public virtual Job VersionOf { get; set; }

    public int JobVersion { get; set; } = 1;

    [ForeignKey("Parent")]
    public int? ParentId { get; set; }
    public virtual Job Parent { get; set; }

    public virtual List<Job> Jobs { get; set; } = new List<Job>();

    public void AddJob(Job job)
    {
        job.Parent = this;
        Jobs.Add(job);
    }
}

public enum TimeUnit
{
    Seconds,
    Minutes,
    Hours,
    Days,
    Weeks,
    Months,
    Years
} 

我将它们添加到这样的上下文中:

        DatabaseContext dbContext = new DatabaseContext();

        Job PK3 = new Job()
        {
            Code = "PK3",
            Description = "Scan Item",
            Time = 7,
            UnitOfTime = TimeUnit.Seconds,
        };
        dbContext.Jobs.Add(PK3);

        Job PK4 = new Job()
        {
            Code = "PK4",
            Description = "Pick Item",
            Time = 15,
            UnitOfTime = TimeUnit.Seconds,
        };
        dbContext.Jobs.Add(PK4);

        Job PK5 = new Job()
        {
            Code = "PK5",
            Description = "Walk To Item",
            Time = 60,
            UnitOfTime = TimeUnit.Seconds,
        };
        dbContext.Jobs.Add(PK5);

        Job OP1 = new Job()
        {
            Code = "OP1",
            Description = "Entire Item Pick",
            Quantity = 1,
        };

        OP1.AddJob(PK5);
        OP1.AddJob(PK4);
        OP1.AddJob(PK3);

        dbContext.Jobs.Add(OP1);

        try
        {
            int a =  dbContext.SaveChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

您可以在此处查看表格的外观: NULL ID的表

我认为可能与LazyLoading一起使用,已启用:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"<--snip-->");
        optionsBuilder.UseLazyLoadingProxies();
    }

但是,我已删除了此问题,并且此问题继续。

有谁知道如何解决这一问题?

您缺少Parent的作业

Job PK4 = new Job()
{
    Code = "PK4",
    Description = "Pick Item",
    Time = 15,
    UnitOfTime = TimeUnit.Seconds,
    Parent = PK3 //add this line!
};

@David Browne-微软是正确的,问题是EF Core根本不知道如何处理两个外键。 将以下内容添加到我的OnModelCreating中已为我解决了此问题。

我不确定这是否是“正确”的方法,但对我有用。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Job>().HasOne(j => j.Parent);
    modelBuilder.Entity<Job>().HasOne(j => j.VersionOf);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM