簡體   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