Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 创建独立实体框架DbContext实体_C# 4.0_Entity Framework 5_Dbcontext - Fatal编程技术网

C# 4.0 创建独立实体框架DbContext实体

C# 4.0 创建独立实体框架DbContext实体,c#-4.0,entity-framework-5,dbcontext,C# 4.0,Entity Framework 5,Dbcontext,因此,我正在开发一个应用程序,它将从一个数据库中选择数据,并根据创作数据库中“发布”表中包含的信息更新相同的数据库。我需要获取一个未连接到“创作”上下文的对象,以便将其添加到“交付”上下文中 目前我正在使用 object authoringRecordVersion = PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).Find(record.RecordId); object deliveryRecordVe

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

目前我正在使用

object authoringRecordVersion = PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).Find(record.RecordId);
object deliveryRecordVersion = PublishingFactory.DeliveryContext.Set(recordType.Entity.GetType()).Find(record.RecordId));
归还我的记录。然后,如果“deliveryRecordVersion”为空,我需要在“PublishingFactory.DeliveryContext”中插入“authoringRecordVersion”。但是,该对象已连接到“PublishingFactory.AuthoringContext”,因此不允许对“PublishingFactory.DeliveryContext”调用Add()方法

我有权访问PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).AsNoTracking()

但是没有办法从这里得到我需要的具体记录。 有什么建议吗

更新:

我相信我找到了解决办法。它第一次不起作用,因为我在设置.State=EntityState.Distached时引用了错误的对象; 下面是一个完全正确的方法,它可以按预期工作

    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;
        }
    }
private void PushToDelivery(IEnumerable recordsToPublish)
{
string recordEntity=string.Empty;
DbEntityEntry recordType=null;
//循环查看recordsToPublish,查看该记录是否存在于传递中。如果存在,则“更新”该记录
//否则“添加”记录。
foreach(recordsToPublish中的var记录)
{
if(recordEntity!=record.Entity)
{
recordType=PublishingFactory.DeliveryContext.Entry(ObjectText.GetEntityOfType(record.Entity));
}
如果(记录类型==null)
{
继续;
////抛出新的NullReferenceException(
////Format(“无法识别存储在record.Entity:{0},record.Entity)中的对象类型”);
}
//从适当的类型表的创作上下文中获取记录
object authoringRecordVersion=PublishingFactory.AuthoringContext.Set(recordType.Entity.GetType()).Find(record.RecordId);
//从适当的类型表中的传递上下文获取记录
object deliveryRecordVersion=PublishingFactory.DeliveryContext.Set(recordType.Entity.GetType()).Find(record.RecordId);
//发生错误,未找到符合中发布表的Id和类型的记录
//创作表
如果(authoringRecordVersion==null)
{
继续;
}
如果(deliveryRecordVersion!=null)
{
//更新记录
PublishingFactory.DeliveryContext.Entry(deliveryRecordVersion).CurrentValues.SetValues(authoringRecordVersion);
PublishingFactory.DeliveryContext.Entry(deliveryRecordVersion).State=EntityState.Modified;
PublishingFactory.DeliveryContext.SaveChanges();
}
其他的
{
//插入新记录
PublishingFactory.AuthoringContext.Entry(authoringRecordVersion).State=EntityState.Distached;
PublishingFactory.DeliveryContext.Entry(authoringRecordVersion).State=EntityState.Added;
PublishingFactory.DeliveryContext.SaveChanges();
}
recordEntity=record.Entity;
}
}

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

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

为什么“不可能”
.Single(a=>a.ID==record.RecordId)1,因为Single()不是.AsNoTracking之后的选项。第二,因为a.Id。不可用,因为objecttype“recordType.Entity.GetType()”类型在设计时不可用。谢谢Gret,这正是我最后要做的。我最初将错误的对象传递给了.State=Entity.Added,因此它失败了,但一旦更正,它就按预期工作了