I'm using EF to communicate with my database.
I made a function import mapping so I can call a Stored Procedure. By on run time I get a error saying "Method not found" referring the method that was created to invoke the SP.
I'm assuming that is an error during the generation on run time.
The question is: Is there any way to see what's going on during the execution time generation so I can find the problem?
Related
I would like to understand how does EF creates a return model from a stored procedure.
I know how to get the return model and even how to customize it but I couldn't find any documentation or explanation on how does EF really get the Return Model from a complicated SP.
I mean, Does it need to actually run the SP when creating the model? (I guess not, How would it know what parameters to use).
Is there any metadata in a system table that contain the return type?
Does it use the execution plan somehow?
Any insight would be appreciated.
In order to get the type returned from the Stored Procedure, EntityFramework runs the SP.
Well, if there is an error in running the SP, then EF fails to get the returned type, and then EF determines the type int. It is not always easy to understand what is wrong with the SP.
Well, I found a way to understand that. This is done by running the "SQL Server Profiler" tool, where you can trace the SP as how EF ran it, and so you can understand what the error is.
By the way, it is very interesting to follow the SP that EF runs. You can learn a lot from this !.
If you are unfamiliar with the SQL Server Profiler tool, you can read about it here. And here you can see an example of how to use it:
How to trace with SQL Server profiler
We have a huge codebase using EF and occasionally an error occurs which keeps any additional EF commands from executing. Our code uses DI so the services should be getting their own instance of the data context. But it seems like somewhere something is going on where if an error occurs on the context, then any further EF calls will fail and will just throw the original error message. So it appears that if it fails on an insert, update, or delete, then the next EF call that is made from another place will try to first execute the failing command again and it just continues to error until we stop and restart the application.
So, is there a way in our error handler code that intercepts all exceptions in the application to get any instance of the data context and reset it so that it doesn't continue to error on any later calls to it?
I'm trying to turn on use of cached entity models (after reading https://mirkomaggioni.com/2018/01/06/performance-improvements-in-entity-framework-6-2/) along with Npgsql provider.
dbconfiguration:
Class NpgsqlConfiguration
Inherits System.Data.Entity.DbConfiguration
Public Sub New()
Dim provider = "Npgsql"
SetProviderServices(provider, Npgsql.NpgsqlServices.Instance)
SetProviderFactory(provider, Npgsql.NpgsqlFactory.Instance)
SetDefaultConnectionFactory(New Npgsql.NpgsqlConnectionFactory())
SetModelStore(New DefaultDbModelStore(Directory.GetCurrentDirectory()))
End Sub
End Class
At the first run Edmx model is generated, with valid content.
However on the second run exception occurs, when i try to get access to some db sets from the context
ctx.Set(Of EntityClass).ToList()
System.ArgumentException: 'The path is not of a legal form.'
How can i overcome this? Thanks for the help.
Entity framework 6.2.0, Ngpsql 4.0.2, ef.npgsql 3.2.0
I'm using Costura.Fody for the solution, and when i've disabled it, EF started to load the generated model.
Hovewer I've didn't notice any significant performance boost :/
calling the first query with no edmx model takes about 2.4s
calling the first query with edmx model takes about 2.2s
I have 46 entities (tables) in the model, may be it is too small amount to find the effect of caching..
When I create my context using a connection string name, that connection is used as expected, but not by the code running Migrations!
That code creates a new context with the parameter-less constructor which then obviously uses its default connection resolving mechanism and thus connects to a different database!!
This seems very undesirable default behavior to me. Why does Migrations not use the current context, or at least the same connection string as the current context?
Am I missing something or is this a flaw in Code First Migrations? And what is the best way to work around this? Implementing IDbContextFactory? Or do somting with Database.SetInitializer in the context constructor?
I actually ended up querying my database without any exceptions occurring (since I did not run any code relying on the migration), and at the same time secretively creating a completely new database in SQLEXPRESS.
Version used is Entity Framework 6 with .NET 4
To prove my findings I added some screenprints from the debugger. The first one shows that the connection taken from connection-string SiteSqlServer is actually used. The second one shows that the connection used in the database initialization code is different.
The third shows the stacktrace at the time of the screenprints.
(Hopfully the prints are readable, zooming in helps)
I am getting the following error when using the RequiredIf attribute in the entity while initializing the database using EF5 code first.
System.Data.DataException: An exception occurred while initializing the database.
See the InnerException for details. ---> System.Data.Entity.Validation.DbEntityValidationException:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
If I comment the [RequiredIf] attribute code first build the database successfully. Any idea?
Update:
I step through the debugger and found that it's adding a validation error. How do I switch this off while initializing the database using code first.
The problem was occurring during the seeding. The requiredIf validation says if the ApplicationStatus is recommended i.e. 2 then the DecisionDate is mandatory. So I added the DecisionDate property to the seed function in my context and it worked. Thank you everybody to looking at the issue anyway.
Lesson learnt is if you put validation in you domain objects then seed data accordingly.