【问题标题】:Creating a unattached Entity Framework DbContext entity创建未附加的实体框架 DbContext 实体
【发布时间】:2013-04-03 20:36:02
【问题描述】:

因此,我正在开发一款应用程序,该应用程序将从一个数据库中选择数据,并根据创作数据库中“发布”表中包含的信息更新相同的数据库。我需要获取一个未连接到“创作”上下文的单个对象,以便将其添加到我的“交付”上下文中。

目前我正在使用

object authoringRecordVersion = PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).Find(record.RecordId);
object deliveryRecordVersion = PublishingFactory.DeliveryContext.Set(recordType.Entity.GetType()).Find(record.RecordId));

返回我的记录。然后,如果“deliveryRecordVersion”为空,我需要将“authoringRecordVersion”插入“PublishingFactory.DeliveryContext”。但是,该对象已经连接到“PublishingFactory.AuthoringContext”,因此它不允许在“PublishingFactory.DeliveryContext”上调用 Add() 方法。

我可以访问PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).AsNoTracking()

但是没有办法从这里获得我需要的特定记录。 有什么建议吗?

更新:

我相信我找到了解决方案。第一次没有用,因为我在设置 .State = EntityState.Detached; 时引用了错误的对象; 这是按预期工作的完整更正方法

    private void PushToDelivery(IEnumerable<Mkl.WebTeam.UWManual.Model.Publication> recordsToPublish)
    {
        string recordEntity = string.Empty;
        DbEntityEntry recordType = null;

        // Loop through recordsToPublish and see if the record exists in Delivery. If so then 'Update' the record
        // else 'Add' the record.
        foreach (var record in recordsToPublish)
        {
            if (recordEntity != record.Entity)
            {
                recordType = PublishingFactory.DeliveryContext.Entry(ObjectExt.GetEntityOfType(record.Entity));
            }

            if (recordType == null)
            {
                continue;
                ////throw new NullReferenceException(
                ////    string.Format("Couldn't identify the object type stored in record.Entity : {0}", record.Entity));
            }

            // get the record from the Authoring context from the appropriate type table
            object authoringRecordVersion = PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).Find(record.RecordId);

            // get the record from the Delivery context from the appropriate type table
            object deliveryRecordVersion = PublishingFactory.DeliveryContext.Set(recordType.Entity.GetType()).Find(record.RecordId);


            // somthing happened and no records were found meeting the Id and Type from the Publication table in the 
            // authoring table
            if (authoringRecordVersion == null)
            {
                continue;
            }

            if (deliveryRecordVersion != null)
            {
                // update record
                PublishingFactory.DeliveryContext.Entry(deliveryRecordVersion).CurrentValues.SetValues(authoringRecordVersion);
                PublishingFactory.DeliveryContext.Entry(deliveryRecordVersion).State = EntityState.Modified;
                PublishingFactory.DeliveryContext.SaveChanges();
            }
            else
            {
                // insert new record
                PublishingFactory.AuthoringContext.Entry(authoringRecordVersion).State = EntityState.Detached;

                PublishingFactory.DeliveryContext.Entry(authoringRecordVersion).State = EntityState.Added;
                PublishingFactory.DeliveryContext.SaveChanges();
            }

            recordEntity = record.Entity;
        }
    }

【问题讨论】:

  • 为什么“不可能”? .Single(a =&gt; a.ID == record.RecordId);?
  • 第一个因为 Single() 在.AsNoTracking 之后不是一个选项。第二,因为 a.Id。不可用,因为对象类型“recordType.Entity.GetType()”类型在设计时不可用。

标签: c#-4.0 entity-framework-5 dbcontext


【解决方案1】:

正如您在评论中所说,您不能使用 .Single(a =&gt; a.ID == record.RecordId) 的原因是 ID 属性在设计时是未知的。所以你可以做的是通过Find 方法获取实体,然后将其从上下文中分离出来:

PublishingFactory.AuthoringContext
   .Entry(authoringRecordVersion).State = EntityState.Detached;

【讨论】:

  • 感谢 Gret,这正是我最终所做的。我最初将错误的对象传递给 .State = Entity.Added 所以它失败了,但一旦纠正它就可以按预期工作
猜你喜欢
  • 2016-11-09
  • 2019-03-13
  • 2014-05-10
  • 2017-05-14
  • 1970-01-01
  • 2014-02-14
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多