0

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

目前我正在使用

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

1 回答 1

1

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

PublishingFactory.AuthoringContext
   .Entry(authoringRecordVersion).State = EntityState.Detached;
于 2013-04-03T22:10:21.370 回答