简体   繁体   English

EF Core不保存ParentId

[英]EF Core not saving ParentId

I am trying to build up a structure of 'Job' objects. 我正在尝试构建“作业”对象的结构。 These are used to describe an entire process a person would need to do. 这些用来描述一个人需要做的整个过程。

For example. 例如。

If the Job had the description "Make Coffee", it would have a List of other jobs that when done all together result in making a coffee. 如果该作业的描述为“煮咖啡”,则该作业将具有其他作业列表,这些作业一起完成后就可以制作咖啡。

An example structure might be: 一个示例结构可能是:

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

It should also be noted, each Job has a version number; 还应注意,每个Job都有一个版本号; and VersionOfID. 和VersionOfID。 This is in case a job needs to be changed, only one version is 'Live' and is the one that will be used. 这是在需要更改工作的情况下,只有一个版本是“ Live”,并且将被使用。

I am adding objects to the EF Core DBContext, and before I call SaveChanges, I can see the structure correctly; 我将对象添加到EF Core DBContext中,在调用SaveChanges之前,我可以正确看到该结构。 However, once the context is closed/reloaded the jobs no longer reference each other. 但是,一旦关闭/重新加载上下文,作业将不再相互引用。 I can see in SQL Management studio the ParentID is null for all the objects. 我可以在SQL Management Studio中看到所有对象的ParentID为null。

The class for Job is as follows: 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
} 

I am adding them to the context like this: 我将它们添加到这样的上下文中:

        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;
        }

You can see how the table looks here: 您可以在此处查看表格的外观: NULL ID的表

I thought it might be todo with LazyLoading, which I have enabled with: 我认为可能与LazyLoading一起使用,已启用:

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

However, I have removed this and this issue continues. 但是,我已删除了此问题,并且此问题继续。

Does anyone know how to fix this? 有谁知道如何解决这一问题?

You are missing the assignment of Parent 您缺少Parent的作业

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

@David Browne - Microsoft is correct, the issue was EF Core simply didn't know how to handle both Foreign Keys. @David Browne-微软是正确的,问题是EF Core根本不知道如何处理两个外键。 Adding the following to my OnModelCreating has resolved this issue for me. 将以下内容添加到我的OnModelCreating中已为我解决了此问题。

I'm not sure if this is the 'Correct' way to do it, but it works for me. 我不确定这是否是“正确”的方法,但对我有用。

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