I'm using the Entity Framework (.NET 4.0) with SQLite as the underlying database and I get an exception when I try to commit some changes to the database:
The underlying provider failed on Commit.
The stack trace is:
System.Data.EntityException: The underlying provider failed on Commit. ---> System.Data.SQLite.SQLiteException: The database file is locked
database is locked
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavi
or behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at System.Data.SQLite.SQLiteTransaction.Commit()
at System.Data.EntityClient.EntityTransaction.Commit()
--- End of inner exception stack trace ---
at System.Data.EntityClient.EntityTransaction.Commit()
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at MySystem.MySystemService.UpdateFollowersAndUsers() in C:\Path\To\MySystem\MySystemService.cs:line 295
My code is pretty simple:
// Gets more followers for the competitor and updates the database
List<Follower> moreFollowers = GetMoreFollowers(competitor);
// Add the new followers to the database
using (MySystemEntities db = new MySystemEntities())
{
try
{
foreach (Follower f in moreFollowers)
{
db.AddToFollowers(f);
}
db.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
This snippet of code is inside MyService, it comes in with a batch of 5,000 followers and it's getting the above exception. However, when I pull the same snippet of code out into the Main function and just manually add a few followers, then it no longer throws the exception and the followers are successfully added to my database. Apparently the database file is locked, what may be causing this issue?
I just found out the answer in another SO question: 'The database file is locked' with System.Data.SQLite
Ironically, the OP answers his own question too :).
Related
My senario is: We have a production and an integration database and I wrote a tool to migrate some of the data of the production database to the integration database. For this I use: Entity Framework 6.0, .NET-Framework 4.5.2 and the databases are MS SQL Server Standard (64-bit) 13.0.5102.
My problem: While saving the deletion of all data in the integration database, the SaveChanges method throw an DbUpdateConcurrencyException with the error message
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
The following is the code that through the exception:
using (var prod = new DbProdContext())
{
using (var #int = new DbIntContext())
using (var transaction = #int.Database.BeginTransaction(IsolationLevel.Serializable))
{
try
{
var stack = BuildStack(#int, ...); // buikds a stack of the tables where the latest pushed tables depdend only on already pushed tables
// Deleting everything in db
using (var x = log4net.NDC.Push("Deleting old content"))
{
foreach (var table in stack.Reverse())
{
var destSet = table.DestSet;
destSet.RemoveRange(destSet);
}
log.Info("Saving Changes...");
#int.SaveChanges(); // <-- throw exception
log.Info("Completed Saving Changes");
}
// code to insert the data
transaction.Commit();
}
catch (Exception ex)
{
// log
transaction.Rollback();
}
}
}
Note: The variable stack is a stack of the table in the integration database where for any given table t all table which t depends on are pushed onto the stack before t is pushed onto the stack.
Seoncond note: I tested the code with migration the data from a local test clone of the production database into the integration database which never throw this exception.
Does any body know how to prevent this error or what caused it.
Third note: The integration database it currently note in use to there could not be a concurrent change to the database from a different process.
Should I maybe just create sql drop statements for the tables instead of using destSet.RemoveRange(destSet)
Should I maybe just create sql drop statements for the tables instead of using destSet.RemoveRange(destSet)
Yes. You should do that. But if you don't want to recreate the tables, it should be DELETE or TRUNCATE TABLE instead of DROP TABLE.
This happens on ReportDocument.Export() for a PDF generation.
But only sporadically/randomly.
In the overall picture it's not that bad, 1 or 2 errors per day (+10k request), but still this bugs me.
2 different kinds of errors:
CrystalDecisions.CrystalReports.Engine.LogOnException: Database logon failed.
CrystalDecisions.CrystalReports.Engine.LogOnException: Error in File < Reportname >.rpt
Unable to connect: incorrect log on parameters
I see that these errors are quite common in the community, i've found lots of sites with solutions to these error, i've tried a lot of them, but none have worked so far (one exception i'll mention below).
Facts
Version used is 13.0.21.2533 (SAP Crystal Reports runtime engine for .NET Framework (32 and 64 bits))
The reports generated work in all environments (even prod)
Some reports randomly/sporadically fail. Although the same (request) report can be generated almost immediately working fine.
No report viewer.
The fact that they work in prod, for me, discards many of the proposed solutions. Regardless i tried/checked them.
No connection to DB, i'm passing a DataSet as the DataSource of the report (multiple tables inside)
I use an "Using" pattern/syntax. I also tried explicitly disposing on Finally (isntead of using)
finally
{
report.Database.Dispose();
report.Close();
report.Dispose();
}
Testing/Replicating
To be able to replicate the issue, i use SOAP UI to Load test the request (1 minute, 2 threads, as many (exactly the same) requests as possible) and from time to time, one fails.
Error 1. Database Logon failed
This one is partially solved, i changed from setting the datasource this way:
report.SetDataSource(rawData);
to setting it for each table in the datasource, more or less like this:
foreach (DataTable dataTable in rawData.Tables)
{
foreach (CrystalDecisions.CrystalReports.Engine.Table table in report.Database.Tables)
{
if (table.Name == dataTable.TableName)
{
table.SetDataSource(dataTable);
break;
}
}
}
This fix removes almost entirely this error, almost.
I need more aggressive Load testing to get it to fail once sporadically, while previously it failed ,once or twice, each and every time i load tested it.
Error 2. Error in File < Reportname >.rpt. Unable to connect: incorrect log on parameters
With the fix above, there are also few instances of this error (1 or 2 exceptions), also need more aggressive load testing to happen, but occurs more than the above one. before it failed 3 to 7 times per load test.
Errors
An exception is thrown:
>>>>> Exception details=CrystalDecisions.CrystalReports.Engine.LogOnException: Database logon failed.
---> System.Runtime.InteropServices.COMException: Database logon failed.
at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
--- End of inner exception stack trace ---
at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export()
at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export()
at < mynamespace >.RptUtil.GeneratePDF(DataSet rawData, ...
the other one
An exception is thrown:
>>>>> Exception details=CrystalDecisions.CrystalReports.Engine.LogOnException: Error in File <
report name >_{1E5E29F7-E5A2-43EA-90E5-D62ECAA19A01}.rpt:
Unable to connect: incorrect log on parameters. ---> System.Runtime.InteropServices.COMException: Error in File <
report name >_{1E5E29F7-E5A2-43EA-90E5-D62ECAA19A01}.rpt:
Unable to connect: incorrect log on parameters.
at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
--- End of inner exception stack trace ---
at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export()
at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export()
at < mynamespace >.RptUtil.GeneratePDF(DataSet rawData, ...
Getting error saving the object in the database stage .I'm waiting for a suggestion for a solution from those who have information on the subject.
I checked my connection strings actually dont have much idea to try.
public void SaveNew(Person p)
{
using (var context = new FeatureContext())
{
context.People.Add(p);// error line
context.SaveChanges();
}
}
}
exception
System.Data.DataException: 'An exception occurred while initializing the database. See the InnerException for details.'
SqlException: Cannot attach the file 'C:\Users\ertug.dilek\source\repos\AddFeatures\WebApplication1\App_Data\AddFeatures.Model.FeatureContext.mdf'
as database 'AddFeatures.Model.FeatureContext'.
I searched a lot trying to find why am I getting this error but no help, can you please advise.
I am getting the following error when executing a linq query
var test = (Linq query).ToList<Testdata>();
System.Data.EntityException: An error occurred while closing the
provider connection. See the inner exception for details. --->
Devart.Data.Oracle.OracleException: OCI invalid handle.
at Devart.Data.Oracle.a1.c(Int32 A_0) at Devart.Data.Oracle.aq.d()
at Devart.Data.Oracle.OracleInternalConnection.a(Boolean A_0)
at Devart.Common.DbConnectionInternal.CloseInternalConnection()
at
Devart.Data.Oracle.OracleInternalConnection.CloseInternalConnection()
at Devart.Common.DbConnectionInternal.Close()
at Devart.Data.Oracle.OracleInternalConnection.Close()
at Devart.Common.DbConnectionBase.Close()
at Devart.Data.Oracle.OracleConnection.Close()
at System.Data.EntityClient.EntityConnection.StoreCloseHelper() ---
End of inner exception stack trace ---
at System.Data.EntityClient.EntityConnection.StoreCloseHelper()
at System.Data.EntityClient.EntityConnection.CloseHelper()
at System.Data.EntityClient.EntityConnection.Close()
at System.Data.Objects.ObjectContext.ReleaseConnection()
at System.Data.Common.Internal.Materialization.Shaper`1.Finally()
at
ystem.Data.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.Dispose()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Please send us a small test project and DDL/DML script for reproducing the issue in our environment. Also specify the versions of your Oracle Server and Oracle Client.
Issue was due to pool reaching max capacity
Why am I getting an InvalidOperationException when adding orders to an Oracle 11gR2 Database using EF 5?
I'm consuming an order feed from Sears.com and adding the order data into an Oracle database using Entity Framework 5 from within VS 2013 professional. Everything works as expected up to order number 835 at which point it throws an exception.
The InvalidOperationException gets thrown by a call to SaveChanges(). The InvalidOperationException should only be thrown if the context has been disposed per the dbContext class:
//
// Summary:
// Saves all changes made in this context to the underlying database.
//
// Returns:
// The number of objects written to the underlying database.
//
// Exceptions:
// System.InvalidOperationException:
// Thrown if the context has been disposed.
public virtual int SaveChanges();
My code is setup as follows:
using (Entities rds = new Entities())
{
foreach (poresponsePurchaseorder po in newSears.purchaseorder)
{
var searsOrders = new SEARS_COM_ORDERS();
searsOrders.ORDER_CONFIRMATION_NUMBER = po.customerorderconfirmationnumber.ToString() ?? "missing data";
searsOrders.CUSTOMER_EMAIL = po.customeremail ?? "missing data";
...
rds.SEARS_COM_ORDERS.Add(searsOrders);
foreach (poresponsePurchaseorderPoline poLine in po.poline)
{
var searsOrderItems = new SEARS_COM_ORDER_ITEMS();
searsOrderItems.PO_NUMBER = (int)po.ponumber;
searsOrderItems.LINE_NUMBER = (int)poLine.polineheader.linenumber;
...
rds.SEARS_COM_ORDER_ITEMS.Add(searsOrderItems);
}
...
try
{
int y = rds.SaveChanges();
Console.WriteLine(y + " Records added");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey();
}
}
}
a call to Entities is not made anywhere else, nor is Dispose(); called at any time (aside from the implicit call during the termination of the using statement)
The exception is:
System.InvalidOperationException: The changes to the database were committed successfully, but an error occurred while updating the object context. Th
e ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict wit
h another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at MarketplaceIntegrator.Utilities.Marketplaces.SearsCom.SearsTests.GetSearsOrders(String from, String to) in c:\Users\roberth\Programming_Projects
\VisualStudio\vsOnline\MarketplaceIntegrator\Utilities\Marketplaces\SearsCom\SearsTests.cs:line 201
Any ideas?
--Edit--
It seems my database model was referencing old primary keys used during the initial development of the database, updating the model from the database resulted in a weird hybrid set of primary keys being added to the model itself.
The only primary key on both my order items table as well as the orders table should be ID, which is auto incremented via a sequence. The model instead added PO_NUMBER and LINE_NUMBER in addition to ID on the order items table and PO_NUMBER and ID to the orders table as shown on the edmx:
<EntityType Name="SEARS_COM_ORDER_ITEMS">
<Key>
<PropertyRef Name="ID" />
</Key>
<EntityType Name="SEARS_COM_ORDERS">
<Key>
<PropertyRef Name="ID" />
</Key>
however in the SSDL it correctly lists the keys.
Rerunning the program after the changes results in the following error. So now the question becomes **Why does the EDMX show different data than the underlaying SSDL and trigger a new error? **
New exception:
System.Data.Entity.Infrastructure.DbUpdateException: Error retrieving values from ObjectStateEntry. See inner exception for details. ---> System.Data.
UpdateException: Error retrieving values from ObjectStateEntry. See inner exception for details. ---> System.Data.MappingException:
RDS.msl(80,10) : error 3002: Problem in mapping fragments starting at line 80:Potential runtime violation of table SEARS_COM_ORDER_ITEMS's keys (SEARS
_COM_ORDER_ITEMS.ID): Columns (SEARS_COM_ORDER_ITEMS.ID) are mapped to EntitySet SEARS_COM_ORDER_ITEMS's properties (SEARS_COM_ORDER_ITEMS.ID) on the
conceptual side but they do not form the EntitySet's key properties (SEARS_COM_ORDER_ITEMS.ID, SEARS_COM_ORDER_ITEMS.LINE_NUMBER, SEARS_COM_ORDER_ITEM
S.PO_NUMBER).
RDS.msl(97,10) : error 3002: Problem in mapping fragments starting at line 97:Potential runtime violation of table SEARS_COM_ORDERS's keys (SEARS_COM_
ORDERS.ID): Columns (SEARS_COM_ORDERS.ID) are mapped to EntitySet SEARS_COM_ORDERS's properties (SEARS_COM_ORDERS.ID) on the conceptual side but they
do not form the EntitySet's key properties (SEARS_COM_ORDERS.ID, SEARS_COM_ORDERS.PO_NUMBER).
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGenerateViews(StorageEntityContainerMapping entityContainerMap, Dictio
nary`2 resultDictionary)
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGetGeneratedViews(EntityContainer container)
at System.Data.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
at System.Data.Common.Utils.Memoizer`2.Result.GetValue()
at System.Data.Common.Utils.Memoizer`2.Evaluate(TArg arg)
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.GetGeneratedView(EntitySetBase extent, MetadataWorkspace workspace, StorageMappi
ngItemCollection storageMappingItemCollection)
at System.Data.Mapping.Update.Internal.ViewLoader.InitializeEntitySet(EntitySetBase entitySetBase, MetadataWorkspace workspace)
at System.Data.Mapping.Update.Internal.ViewLoader.SyncInitializeEntitySet[TArg,TResult](EntitySetBase entitySetBase, MetadataWorkspace workspace, F
unc`2 evaluate, TArg arg)
at System.Data.Mapping.Update.Internal.ViewLoader.SyncContains[T_Element](EntitySetBase entitySetBase, MetadataWorkspace workspace, Set`1 set, T_El
ement element)
at System.Data.Mapping.Update.Internal.ExtractorMetadata..ctor(EntitySetBase entitySetBase, StructuralType type, UpdateTranslator translator)
at System.Data.Mapping.Update.Internal.UpdateTranslator.GetExtractorMetadata(EntitySetBase entitySetBase, StructuralType type)
at System.Data.Mapping.Update.Internal.ExtractorMetadata.ExtractResultFromRecord(IEntityStateEntry stateEntry, Boolean isModified, IExtendedDataRec
ord record, Boolean useCurrentValues, UpdateTranslator translator, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
at System.Data.Mapping.Update.Internal.RecordConverter.ConvertStateEntryToPropagatorResult(IEntityStateEntry stateEntry, Boolean useCurrentValues,
ModifiedPropertiesBehavior modifiedPropertiesBehavior)
--- End of inner exception stack trace ---
at System.Data.Mapping.Update.Internal.RecordConverter.ConvertStateEntryToPropagatorResult(IEntityStateEntry stateEntry, Boolean useCurrentValues,
ModifiedPropertiesBehavior modifiedPropertiesBehavior)
at System.Data.Mapping.Update.Internal.ExtractedStateEntry..ctor(UpdateTranslator translator, IEntityStateEntry stateEntry)
at System.Data.Mapping.Update.Internal.UpdateTranslator.LoadStateEntry(IEntityStateEntry stateEntry)
at System.Data.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager()
at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at MarketplaceIntegrator.Utilities.Marketplaces.SearsCom.SearsTests.GetSearsOrders(String from, String to) in c:\Users\roberth\Programming_Projects
\VisualStudio\vsOnline\MarketplaceIntegrator\Utilities\Marketplaces\SearsCom\SearsTests.cs:line 200
--Edit 2--
Further research appears to require manual editing of the edmx:
per Microsoft:
The conceptual model will be updated only for objects that are added to the database. All other changes to the conceptual model must be made manually.
Manually changing the properties for the referenced fields to set entity key to false as brought me back to the original error again.
The actual fix for this encompasses multiple items. In my case the following caused the error:
Changing the primary key in the database after the model was generated.
If this occurs the following steps need to be taken to correct this:
Update the model from the database
fix the primary keys on the edmx by removing old references (setting Entity Key to false on the properties and changing StoreGeneratedPattern to none)
ensure the new key has Entity Key set to true and StoreGeneratedPattern is set to Identity
validate information is correct on the SSDL by right clicking the edmx and choosing open with text editor and editing if required to show the correct keys. Specifically look for StoreGeneratedPattern="Identity" on the wrong key and move it to the right key if not already present. The SSDL should mirror the EDMX model and errors after updating the previous items likely means that the SSDL differs from the model.
This exception was misleading to say the least in the fact that the documentation for the SaveChanges() call indicated it gets thrown when the dbContext is closed, while the error message indicates a duplicate key when neither are in fact true (as far as I am concerned anyways).
I know this post was long winded, but hopefully it helps someone else troubleshoot the same issue.