I have a database that is accessed by two applications. The first is an ASP.NET MVC application which just reads data out of the database. The second is a C# Console application which reads and writes to the database.
The problem I'm experiencing is that when the console application performs a mass update of up to several hundred rows, the MVC application will crash and burn. If I purge the entire database and refill it, the MVC application comes back to life, but anytime the console application needs to perform an update the MVC app will crash.
What I assume is happening is that the DataContext in the MVC application sees that the data has changed and doesn't know what to do. Or at least that's what I think is happening because the exception being thrown, System.Web.HttpUnhandledException, is really vague and is being caught by the OnException Controller override.
I have tried several fixes, but none of which did anything:
Restarting IIS or the entire server does not help.
Overriding the OnActionExecuting in the Controller and telling it to create a new DataContext does not work.
Overriding the OnActionExecuting in the Controller and telling it to refresh the DataContext does not work. (Although I'm not 100% sure this failed because I did this:)
DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, this);
The compiler let it go through so I assumed it understood what I was trying to do...
(4) Wrapping each ActionResult with a using statement and creating a new DataContext doesn't work.
I would appreciate any suggestions anyone might have on how I should fix this.
Thanks in advance!
Check the inner exception. If the inner exception is null, make sure you are doing a "throw" not a "throw ex" so that the stack trace doesn't get reset.
Related
I've looked at the other solutions regarding this error, but I believe my problem is different. As they do not work.
I have a Azure App Services backend. It's using ASP.NET and Entity.
For some strange reason, I'm unable to insert a new entry into one of my tables.
It's only this one table, nothing has changed on it. I understand why this error is thrown under normal circumstances, but nothing is changing by the time I save my changes.
Something as simple as this, is throwing the error:
var client = new Client() { Id = Guid.NewGuid().ToString(), CompanyID = "guid-fk-in-other-table" };
context.Clients.Add(client);
context.SaveChanges();
When I step through with the debugger, the context.Clients.Add(client); line takes really long to execute and the output window shows a bunch of unhelpful exceptions such as:
Exception thrown: 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' in EntityFramework.dll
The table schema is identical to my DataObject. I'm getting desperate and have no idea how to diagnose from here. Any help is appreciated beyond belief!
Well, after weeks of debugging, finally figured this one out.
There's row-level security implemented on this table. For some reason, Entity is unable to add a row if the current user doesn't pass the access predicate. I'm able to insert rows directly through SQL though without passing the policy, so this contributed to the confusion.
I guess Entity attempts to read the entry?
Anyway,
I then deleted the policy and things worked as they should. The reason they worked before, is because the calling app had cached credentials, satisfying the security policy. Once I cleared them, the issue came up.
There was absolutely nothing I could find in debugger exceptions that would have led me to this conclusion, an exception pointing out the security policy was at fault would have been welcome.
I am using Entity Framework 6 and SQL Server. I am writing some code in a WCF service for saving data when receives a POST http request.
I have done some methods and they work well as expected. But now I'm writing another method.. in debug mode it seems like other, but when I call the saveChanges method I get this:
{"A cursor with the name 'FldCursor1' does not exist.\r\nThe statement has been terminated."}
But I don't even know what is a cursor... of course I have searched in the web, but I don't know why there is this message. Further, the name FldCursor1 isn't written in my code, I've searched it.
Do you know why this can happen?
I don't have used cursors anywhere... why this error?
If you need I can post some of my code, you have only to task. So, please let me know if you need code or if you have useful ideas for this problem.
UPDATE: when I get the error, I'm trying to add some new records in a table.
I am developing a web application on ASP.NET with c#, when a trace error occurs on the website I store the error in a database from the global.asax using the Application_Error method. This is working fine but there are many errors occurring like client disconnected. How can I handle this error from global.asax it will not be stored in the database?
Assuming you don't want to track them at all, I would suggest creating a black list of ignorable exceptions and compare against them using the type and partial message text as the comparison. The black list could be stored in a DB table that you just read on ApplicationStart and cached or you could just hard code them.
You could also extend the solution by storing a count of how often these blacklisted exceptions are getting caught and store them in the application cache. Then have an admin page that could list the count or average frequency etc. in case you need to diagnose if these exceptions are actually occurring due to something valid that should be looked into.
It all comes down to how important these exceptions are to you and if they do become important in the future, how easily you can diagnose the issue with what tools you have at hand.
Over the last few months I have been developing an application using Entity Framework code first and sql server CE for the first time. I have found the combination of the 2 very useful, and compared to my old way of doing things (particularly ADO.NET) it allows for insanely faster dev times.
However, this morning me and some colleagues came across a problem which we have never seen in any documentation regarding SqlServer CE. It cannot handle more than one insert at once!
I was of the opinion that CE may become my database of choice until I came across this problem. The reason I discovered this was in my application I needed to make multiple requests to a web service at once, and it was introducing a bit of a bottleneck so I proceeded to use a Parallel.Invoke call to make the multiple requests.
This was all working fine untill I turned on my applications message logging service. At this point I began to get the following error when making the web requests:
A duplicate value cannot be inserted into a unique index. [ Table name = Accounts,Constraint name = PK__Accounts__0000000000000016 ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into a unique index. [ Table name = Accounts,Constraint name = PK__Accounts__0000000000000016 ]
Strange I thought. And my first recation was that it must be something to do with the DbContext, maybe the DbContext I was using was static or something else in my Repository class was static and causing the problem, but after sniffing around I was certaing it was nothing to do with my code.
I then brought it to the attention of my colleagues and after a while it was decided it must be SqlServer CE, and after us all setting up different test projects attempting to recreate the problem using threads it was recreated almost every time, and when using Sql Server Express the problem wasn't ocurring.
I just think it is a bit strange that CE cannot handle something as simple as this. I mean the problem is not only with threading - are you telling me that it cannot be used for a web application where two users may insert into a table at the same time...INSANITY!
Anyway, just wondering if anyone else has come across this late into a project like me and been shocked (and annoyed) that it works this way? Also if anyone could shed light on why it is limited in this way that would be cool.
It looks like a bug in SQL CE. See http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=641518
I am incorporating DotNetOpenAuth to my asp.net website. I am new with Entity framework.
Database.DataContext.AddToUser(user) located in LogingFrame.aspx.cs does not add a user into the database. User and AuthenticationToken records are filled correctly. The same code from template project, which points to the same database, works fine. Probably I missed something during incorporation. Could you
please point what I have to check? Please let me know if you need any code to be provided.
...does not add a user into the database. User and AuthenticationToken records are filled correctly.
Your question seems to contradict itself, or I'm reading it wrong. It sounds like you're saying no user is added, but then a user is added.
Let me take a stab at it though and let you know how this project template works. A database transaction wraps all database changes within a single HTTP request. It's built into the RelyingPartyLogic assembly to have this behavior. Also, at the end of a successful HTTP request (one that didn't result in an unhandled exception) SaveChanges() is called and the transaction is committed. If an unhandled exception is thrown, the changes and transaction are rolled back. This helps to protect the integrity of your database.
A side-effect of this is, however, that if you're debugging through an "add a user" method and after you see AddToUser executed you jump to the users table to see if it was added, it won't be there because SaveChanges hasn't yet been called and the transaction hasn't been committed yet.
Per standard Entity Framework behavior, SaveChanges must be called for your changes to be persisted in the database, but as I said before, the RelyingPartyLogic library makes this call for you. But you may sometimes need to call SaveChanges yourself in your own code in order to (for example) get the ID of a newly added row. This works even within a transaction before committing it.
Hope that helps.
Did you forget to call DataContext.SaveChanges()?