好的,我不确定我在这里错过了什么,所以就这样吧。我正在使用实体框架填充 MySQL 数据库。我要么更新或向数据库添加新记录。首先,我搜索一条记录,如果它返回 null,那么我知道我必须添加记录。如果搜索返回记录,那么我对记录进行更改。
所以这是一个简短的总结。最初我将配置属性保留为默认值并在需要时运行 SaveChanges(),但是我阅读以加快我的程序将它们设置为 false 更好。当我关闭这些时,我的问题发生了:
context.Configuration.AutoDetectChangesEnabled = false;
context.Configuration.ValidateOnSaveEnabled = false;
下面是给我带来麻烦的代码。 一旦我运行 Attach 方法并设置 Modified 状态,然后运行WFContext.SaveChanges()
System.Data.Entity.Infrastructure.DbUpdateException 就会抛出。 我以为我做得对吗?显然不是...
if (add)
{
WFContext.SecuritiesEntitySet.Add(security);
}
else
{
WFContext.SecuritiesEntitySet.Attach(security);
WFContext.Entry(security).State = EntityState.Modified;
}
我到底在这里想念什么?
这是跟踪:
A first chance exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at MyContext.SaveChanges() in MyContext.cs:line 24
at ContextConnection.RecreateWFContext() in ContextConnection.cs:line 26
at AbstractRecords.CheckRecordCount() in AbstractRecords.cs:line 46
at SecuritiesRecord.Parser() SecuritiesRecord.cs:line 74
An error occurred while updating the entries. See the inner exception for details.
您在跟踪及以后看到的内容的简要细分:
- ContextConnection 是一个静态类,用于管理与 MyContext 的连接。RecreateWFContext 释放当前上下文,然后再次初始化。
- AbstractRecords 在所有记录类都可以使用的方法中提供属性。
- SecuritiesRecord.Parser 解析字符串并加载安全对象中的所有属性。
这是 SecurityInfo 类,security
在上面的问题代码示例中实例化并引用了它。
public partial class SecurityInfo
{
public System.DateTime ImportDate { get; set; }
public string CUSIP { get; set; }
public string Symbol { get; set; }
public string SecurityType { get; set; }
public Nullable<System.DateTime> PriceDate { get; set; }
public Nullable<decimal> PriceClose { get; set; }
public Nullable<decimal> DividendRate { get; set; }
public Nullable<System.DateTime> ExDate { get; set; }
public Nullable<System.DateTime> PayableDate { get; set; }
public string PaymentFrequency { get; set; }
public Nullable<System.DateTime> FirstCallDate { get; set; }
public Nullable<decimal> FirstCallRate { get; set; }
public Nullable<System.DateTime> SecondCallDate { get; set; }
public Nullable<decimal> SecondCallRate { get; set; }
public Nullable<int> Industry { get; set; }
public Nullable<decimal> EquityDividendRate { get; set; }
public Nullable<System.DateTime> EquityRecordDate { get; set; }
public Nullable<System.DateTime> EquityPayableDate { get; set; }
public Nullable<System.DateTime> EquityExDividendDate { get; set; }
public string EquitySplitRate { get; set; }
public string OSISymbol { get; set; }
public Nullable<System.DateTime> OSIExpirationDate { get; set; }
public string OSIOptionType { get; set; }
public Nullable<decimal> OSIStrikePrice { get; set; }
public Nullable<decimal> OptionContractLotSize { get; set; }
public Nullable<decimal> AnnualCouponRate { get; set; }
public Nullable<System.DateTime> MaturityDate { get; set; }
public Nullable<System.DateTime> PaymentDate { get; set; }
public Nullable<decimal> GNMAFactor { get; set; }
public string SPRating { get; set; }
public string MoodyRating { get; set; }
}
更新 我把这些拿出来:
context.Configuration.AutoDetectChangesEnabled = false;
context.Configuration.ValidateOnSaveEnabled = false;
我仍然得到同样的例外。我认为它必须在 ContextConnection 类中。这是它的样子:
public static class ContextConnection
{
private static MyContext WFContext;
public static MyContext GetWFContext()
{
return WFContext;
}
public static void NewWFContext()
{
WFContext = new MyContext();
//WFContext.Configuration.AutoDetectChangesEnabled = false;
//WFContext.Configuration.ValidateOnSaveEnabled = false;
}
public static void RecreateWFContext()
{
WFContext.SaveChanges();
DisposeWFContext();
NewWFContext();
}
public static void DisposeWFContext()
{
WFContext.Dispose();
}
}
当程序启动时,我调用ContextConnection.NewWFContext()
并在 try-catch 块的 finally 部分调用DisposeWFContext
只是为了好衡量。这是 AbstractRecords 类中的方法,当 100 条记录已更改时,我在其中重新创建上下文:
protected void CheckRecordCount()
{
RecordsChangedCount++;
if (RecordsChangedCount == 100)
{
ContextConnection.RecreateWFContext();
RecordsChangedCount = 0;
}
}
任何帮助将不胜感激!
谢谢,贾斯汀