在Entity Framework DbContext中使用Newtonsoft.Json,不会为成员异常解析Type

时间:2015-08-17 16:54:10

标签: c# .net json entity-framework json.net

在我的DbContext课程中,我刚刚添加了对Newtonsoft.Json的一些引用,因为我使用它来存储实体的序列化版本以供审核。

现在,当我运行update-database命令时,我在控制台中收到以下内容。

  

指定' -Verbose'用于查看应用于目标数据库的SQL语句的标志   没有待定的显式迁移   运行种子方法。

     

System.Runtime.Serialization.SerializationException:未解析成员' Newtonsoft.Json.JsonSerializationException,Newtonsoft.Json,Version = 7.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed'。
  在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
  在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
  在System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)
  在System.Data.Entity.Migrations.UpdateDatabaseCommand。<> c__DisplayClass2。< .ctor> b__0()
  在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(操作命令)

     

未解析成员' Newtonsoft.Json.JsonSerializationException,Newtonsoft.Json,Version = 7.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed'。

所有引用都很好,Newtonsoft.Json dll在输出目录中。

有没有人知道如何让这个工作?

由于

史蒂夫

编辑:

public EFDbContext(string nameOrConnectionString = "DbConnectionString") : base(nameOrConnectionString)
{

     IObjectContextAdapter adapter = this;
     adapter.ObjectContext.SavingChanges += ObjectContextOnSavingChanges;

}

/// <summary>
/// Audits the records.
/// </summary>
/// <param name="objectContext">The object context.</param>
public void AuditRecords(ObjectContext objectContext)
{
    IEnumerable<ObjectStateEntry> changes = objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);
    foreach (ObjectStateEntry stateEntryEntity in changes)
    {
        if (!stateEntryEntity.IsRelationship && stateEntryEntity.Entity != null && !(stateEntryEntity.Entity is AuditEntry))
        {
            AuditEntry audit = CreateAuditRecord(stateEntryEntity, objectContext);
            this.AuditEntries.Add(audit);
        }
    }
}

/// <summary>
/// Creates the audit record.
/// </summary>
/// <param name="objectStateEntry">The object state entry.</param>
/// <param name="objectContext">The object context.</param>
/// <returns>AuditEntry.</returns>
public AuditEntry CreateAuditRecord(ObjectStateEntry objectStateEntry, ObjectContext objectContext)
{
    AuditEntry audit = new AuditEntry();

    audit.TableName = objectStateEntry.EntitySet.Name;
    audit.ApplicationUserId = UserId;

    if (objectStateEntry.State == EntityState.Added)
    {//entry is Added 
        audit.NewValue = GetEntryValueInString(objectStateEntry, false);
        audit.Action = AuditAction.Add;
    }
    else if (objectStateEntry.State == EntityState.Deleted)
    {//entry in deleted
        audit.OldValue = GetEntryValueInString(objectStateEntry, true);
        audit.Action = AuditAction.Delete;
    }
    else
    {//entry is modified
        audit.OldValue = GetEntryValueInString(objectStateEntry, true);
        audit.NewValue = GetEntryValueInString(objectStateEntry, false);
        audit.Action = AuditAction.Update;

        IEnumerable<string> modifiedProperties = objectStateEntry.GetModifiedProperties();
        //assing collection of mismatched Columns name as serialized string 
        audit.ChangedColumns = JsonConvert.SerializeObject(modifiedProperties.ToArray());
    }

    return audit;

}

/// <summary>
/// Clones the entity.
/// </summary>
/// <param name="obj">The object.</param>
/// <returns>EntityObject.</returns>
public object CloneEntity(BaseEntity obj)
{
    return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(obj), obj.GetType());
}

/// <summary>
/// Gets the entry value in string.
/// </summary>
/// <param name="entry">The entry.</param>
/// <param name="isOrginal">if set to <c>true</c> [is orginal].</param>
/// <returns>System.String.</returns>
private string GetEntryValueInString(ObjectStateEntry entry, bool isOrginal)
{

    if (entry.Entity is BaseEntity)
    {
        object target = CloneEntity((BaseEntity)entry.Entity);
        foreach (string propName in entry.GetModifiedProperties())
        {
            object setterValue = null;
            if (isOrginal)
            {
                setterValue = entry.OriginalValues[propName];
            }
            else
            {
                setterValue = entry.CurrentValues[propName];
            }

            PropertyInfo propInfo = target.GetType().GetProperty(propName);

            if (setterValue == DBNull.Value)
            {
                setterValue = null;
            }
            propInfo.SetValue(target, setterValue, null);
        }

        return JsonConvert.SerializeObject(target);
    }
    return null;
}

1 个答案:

答案 0 :(得分:1)

有一个引用问题的自引用循环。

从种子方法插入一些基础数据。当我将调试器附加到更新数据库进程时,它实际上给了我正确的异常。在发布此问题之后,我在谷歌搜索它之前,我不知道你可以附加一个调试器。

使用相同的项目打开第二个Visual Studio,放置

if (!System.Diagnostics.Debugger.IsAttached)
    System.Diagnostics.Debugger.Launch();

在ctor中然后能够看到真正的异常。

  

'类型未解决'

消息让我觉得它找不到dll。

感谢您的时间Gert Arnold

这个问题没有意义。