在我的 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.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)的System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)处于System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)at at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action命令)System.Data.Entity.Migrations.UpdateDatabaseCommand . <> c__DisplayClass2 . < . ctor> b__0()处理成员'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 回答
有一个自引用循环导致了这个问题 .
从种子方法插入一些基础数据 . 当我将调试器附加到更新数据库进程时,它实际上给了我正确的异常 . 在发布此问题之后,我在谷歌搜索它之前,我不知道你可以附加一个调试器 .
使用相同的项目打开第二个Visual Studio
在ctor然后能够看到真正的例外 . 该
消息让我觉得它找不到dll .
谢谢你的时间Gert Arnold
这个问题毫无意义 .