![](/img/trans.png)
[英]The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable.
[英]The relationship could not be changed because one or more of the foreign-key properties is non-nullable. (2)
我在 .NET 4.5 應用程序中使用 Entity Framework 6.1.3,在 Oracle 數據庫服務器上使用 Code First 和手工制作的表架構。 大多數事情都運行良好。 對於新函數,在SaveChanges
拋出以下異常:
操作失敗:無法更改關系,因為一個或多個外鍵屬性不可為空。 當對關系進行更改時,相關的外鍵屬性將設置為空值。 如果外鍵不支持空值,則必須定義新關系,必須為外鍵屬性分配另一個非空值,或者必須刪除不相關的對象。
堆棧跟蹤:
它沒有說明問題。 這對我找不到它沒有幫助。 SQL 日志是空的,所以我猜這個問題是在嘗試訪問數據庫之前由 EF 在本地檢測到的。
我的問題是:我該如何調試這個東西? 關於哪個對象中的哪個外鍵具有哪個值但不應該具有它的詳細信息在哪里? 是否有可用於 Entity Framework 的跟蹤日志,其中包含有關已完成操作的內部數據?
情況太復雜了,無法在這里展示,所以請不要要求它。 我想幫助自己擺脫這個,我只是需要幫助。
您可以覆蓋 DBContext 的 SaveChanges 方法,並查看所有屬性的更新/刪除/修改,它將最小化您需要檢查錯誤的列表。 我只需要刪除/分離的那些,但您可以根據需要修改.where 。
public override int SaveChanges()
{
try
{
var debug = false;
if (debug)
{
var modifiedEntities = ChangeTracker.Entries()
.Where(p => p.State == EntityState.Deleted || p.State == EntityState.Detached).ToList();
foreach (var change in modifiedEntities)
{
var entityName = change.Entity.GetType().Name;
System.Diagnostics.Debug.WriteLine(string.Format("Entity {0}", entityName));
}
}
return base.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Debug.WriteLine("Error while Save Changes:");
Debug.WriteLine("Entity {0} has the following validation errors:", eve.Entry.Entity.GetType().Name);
foreach (var ve in eve.ValidationErrors)
{
Debug.WriteLine("Property:{0}, Error: {1}",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
catch (Exception)
{
throw;
}
}
您可以記錄 SQL
using (var context = new BlogContext())
{
context.Database.Log = Console.Write;
// Your code here...
}
您甚至可以像這樣制作自定義日志格式化程序
public class OneLineFormatter : DatabaseLogFormatter
{
public OneLineFormatter(DbContext context, Action<string> writeAction)
: base(context, writeAction)
{
}
public override void LogCommand<TResult>(
DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
Write(string.Format(
"Context '{0}' is executing command '{1}'{2}",
Context.GetType().Name,
command.CommandText.Replace(Environment.NewLine, ""),
Environment.NewLine));
}
public override void LogResult<TResult>(
DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
}
}
假設它是“DbEntityValidationException”,您可以通過自己的日志格式化程序攔截該確切異常的確切異常
public class MyExcpetionCommandInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuting(
DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogIfNonAsync(command, interceptionContext);
}
public void NonQueryExecuted(
DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogIfError(command, interceptionContext);
}
public void ReaderExecuting(
DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
LogIfNonAsync(command, interceptionContext);
}
public void ReaderExecuted(
DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
LogIfError(command, interceptionContext);
}
public void ScalarExecuting(
DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogIfNonAsync(command, interceptionContext);
}
public void ScalarExecuted(
DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogIfError(command, interceptionContext);
}
private void LogIfNonAsync<TResult>(
DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
if (!interceptionContext.IsAsync)
{
Logger.Warn("Non-async command used: {0}", command.CommandText);
}
}
private void LogIfError<TResult>(
DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
if (interceptionContext.Exception.GetType() == typeof(DbEntityValidationException) || typeof(DbEntityValidationException).IsAssignableFrom(interceptionContext.Exception.GetType()) )
{
Logger.Error("Command {0} failed with exception {1}",
command.CommandText, interceptionContext.Exception);
}
}
}
在 Microsoft Logging 和攔截數據庫操作中有一篇很好的文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.