向导航属性添加新项不会创建增量int PK

时间:2019-05-24 13:59:45

标签: c# sql-server entity-framework-core

这是情况:

我有一个服务器,可以为零件创建机器程序。我的基础结构如下:

  • WillyDemandes:请求为零件创建程序
  • WillyMachines(与FK相关的WillyDemandes):每个机器程序 为此部分创建的。
  • WillyResults(与FK有关的WillyMachines):每次警告, 结果,机器的输出。

服务器将处理第一个WillyDemandes:

    static public WillyDemandes GetFirst()
    {

        WillyDemandes willyDemandes;

        using (Info_IndusContext db = new Info_IndusContext())
        {
                willyDemandes = db.WillyDemandes
                    .Include(x => x.IdPartNavigation)
                    .Where(x => x.Statut == Statuts.EnTest.ToString() && x.Username == Environment.UserName)
                    .OrderBy(x => x.Priority)
                    .ThenBy(x => x.Id)
                    .FirstOrDefault();

            if (willyDemandes != null)
            {
                willyDemandes.Statut = Statuts.EnTraitement.ToString();
                willyDemandes.ServerName = Environment.MachineName;
                willyDemandes.DateDebut = DateTime.Now;
                db.SaveChanges();
            }
        }
        return willyDemandes;

    }

然后服务器将新机器添加到零件(将新的WillyMachines添加到WillyDemandes)

    static public WillyMachines Create(WillyDemandes willyDemandes, string MachineName)
    {
        using (Info_IndusContext db = new Info_IndusContext())
        {
            WillyMachines willyMachines = willyDemandes.WillyMachines.Where(x => x.DocumentType == MachineName).FirstOrDefault();
            if (willyMachines == null)
            {
                willyMachines = new WillyMachines() { IdDemande = willyDemandes.Id, DocumentType = MachineName };
                willyDemandes.WillyMachines.Add(willyMachines);
            }

            willyMachines.DateDebut = DateTime.Now;
            willyMachines.DateFin = null;
            willyMachines.Performance = null;
            willyMachines.Statut = Statuts.EnTraitement.ToString();
            db.SaveChanges();

            return willyMachines;
        }
    }

问题是,每当我执行前面的代码时,SaveChanges都说上下文没有更改,willyMachines的ID保持为0,而不是获取数据库应提供给他的增量ID。因此,我做了以下修补程序(作为替代方法)(添加到context.WillyMachines中,而不是导航属性中,并刷新WillyDemandes)

    static public WillyMachines Create(WillyDemandes willyDemandes, string MachineName)
    {
        using (Info_IndusContext db = new Info_IndusContext())
        {
            WillyMachines willyMachines = willyDemandes.WillyMachines.Where(x => x.DocumentType == MachineName).FirstOrDefault();
            if (willyMachines == null)
            {
                willyMachines = new WillyMachines() { IdDemande = willyDemandes.Id, DocumentType = MachineName };
                db.WillyMachines.Add(willyMachines);
            }

            willyMachines.DateDebut = DateTime.Now;
            willyMachines.DateFin = null;
            willyMachines.Performance = null;
            willyMachines.Statut = Statuts.EnTraitement.ToString();
            db.SaveChanges();
            willyDemandes = WillyDemandesController.Refresh(willyDemandes);
            return willyMachines;
        }
    }

    static public WillyDemandes Refresh(WillyDemandes willyDemandes)
    {
        using (Info_IndusContext db = new Info_IndusContext())
        {
            return db.WillyDemandes
                    .Include(x => x.IdPartNavigation)
                    .Include(x => x.WillyMachines).ThenInclude(x => x.WillyResults)
                    .Where(x => x.Id == willyDemandes.Id)
                    .FirstOrDefault();
        }
    }

为什么我的上下文看不到WillyDemandes的变化?我是否应该事先将WillyDemandes附加到上下文?我应该去别的东西吗?我的解决方案可行吗?

谢谢

1 个答案:

答案 0 :(得分:0)

在第一种情况下,没有将对象添加到上下文中。 willyDemandes只是一个变量,如果找不到记录,则包含null。将变量设置为其他值不会修改该内存位置中存储的 pointer

第一个片段等效于:

        WillyMachines willyMachines = willyDemandes.WillyMachines
                                                   .Where(x => x.DocumentType == MachineName)
                                                   .FirstOrDefault();
        if (willyMachines == null)
        {
            var otherMachines = new WillyMachines{ 
               IdDemande = willyDemandes.Id, 
               DocumentType = MachineName 
               WillyMachines.Add(willyMachines);            
               DateDebut = DateTime.Now;
               DateFin = null;
               Performance = null;
               Statut = Statuts.EnTraitement.ToString()
           };
       }
       db.SaveChanges();

应使用db.WillyMachines.Add(willyMachines);将新对象添加到上下文中: