Tim
Tim

Reputation: 1249

Creating a unattached Entity Framework DbContext entity

So I'm working on an app that will select data from one database and update an identical database based on information contained in a 'Publication' Table in the Authoring database. I need to get a single object that is not connected to the 'Authoring' context so I can add it to my 'Delivery' context.

Currently I am using

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

to return my records. Then if the 'deliveryRecordVersion' is null, I need to do an Insert of 'authoringRecordVersion' into 'PublishingFactory.DeliveryContext'. However, that object is already connected to the 'PublishingFactory.AuthoringContext' so it won't allow the Add() method to be called on the 'PublishingFactory.DeliveryContext'.

I have access to PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).AsNoTracking()

but there is no way to get at the specific record I need from here. Any suggestions?

UPDATE:

I believe I found the solution. It didn't work the first time because I was referencing the wrong object on when setting .State = EntityState.Detached; here is the full corrected method that works as expected

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

Upvotes: 0

Views: 349

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109118

As you say in your comment the reason why you can't use .Single(a => a.ID == record.RecordId) is that the ID property is not known at design time. So what you can do is get the entity by the Find method and then detach it from the context:

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

Upvotes: 1

Related Questions