【发布时间】:2015-11-10 09:49:12
【问题描述】:
在我的DbContext 类中,我刚刚添加了一些对Newtonsoft.Json 的引用,因为我使用它来存储用于审计的实体的序列化版本。
现在,当我运行 update-database 命令时,我会在控制台中看到以下内容。
指定“-Verbose”标志以查看应用于目标数据库的 SQL 语句。
没有待处理的显式迁移。
运行种子方法。System.Runtime.Serialization.SerializationException:未解析成员“Newtonsoft.Json.JsonSerializationException,Newtonsoft.Json,版本=7.0.0.0,文化=中性,PublicKeyToken=30ad4fe6b2a6aeed”的类型。
在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
在 System.Data.Entity.Migrations.Design.ToolingFacade.Update(字符串 targetMigration,布尔力)
在 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;
}
【问题讨论】:
-
那么更新数据库试图做什么?如果只添加引用的库,为什么要运行它?
-
我使用 add-migration 添加了一些新的迁移,因此运行 update-database 来应用迁移。我有一个附加到 SavingChanges 的事件,它使用 Json.Net 序列化程序
-
好的,您显然在 Seed 方法中使用了 Json 序列化,您可以展示那部分吗?
-
我已经编辑了我的问题并添加了 OnSavingChanges 代码。种子方法确实插入了一些基础数据,然后调用 SaveChanges,后者将调用 OnSavingChanges。
-
我想我已经找到了问题,只是确认并发布答案。有点误导
标签: c# .net json entity-framework json.net