EF Core不保存ParentId

时间:2018-07-31 20:57:25

标签: c# entity-framework-core

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

例如。

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

示例结构可能是:

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

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

我正在将对象添加到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;
        }

您可以在此处查看表格的外观: table with NULL IDs

我认为可能与启用了以下功能的LazyLoading有关:

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

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

有人知道如何解决此问题吗?

2 个答案:

答案 0 :(得分:0)

您缺少Parent

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

答案 1 :(得分:0)

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

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

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