I am trying to execute a stored procedure in a Database First DB in EF Core 2. Can this be done by using the OnModelCreating method of the Context class?
Related
In EF 6 my work flow was to make all DB changes directly in SQL Server and then manually update/add EF classes to match what's the in the database. What I want to avoid is driving the DB design from code or scaffolding from the DB into EF.
I just want to manually manage everything once the DBContext has been generated.
Is this still possible in EF Core?
I just want to manually manage everything once the DBContext has been generated. Is this still possible in EF Core?
Absolutely. Same as in EF 6 Code First, just create the classes and map them to your database objects.
I can't find examples on how to use EF Database Initializers with EF Core nor can I find a list of EF Database Initializers shipping with EF Core.
Has the concept of Database Initializers become obsolete? What's the current approach of initializing a database ... or not initializing an existing database?
In .Net core you can use functions of Database property form DbContext instance. It allows you to create database (EnsureCreated), destroy database (EnsureDeleted) and migrate to last migration (Migrate). Required data should be placed in migrations. Look at last section of https://learn.microsoft.com/ef/core/managing-schemas/migrations/ for more details.
I need to execute a stored procedure with Entity Framework.
Normally I call it like this:
this.Context.Database.ExecuteSqlCommand("EXEC edi_UploadTransmission");
However, this particular stored procedure includes accessing a linked server.
Since EF wraps ExecuteSqlCommand in a transaction, it is failing, as a linked server is not supported in a transaction (as far as I can tell).
Is there a way to execute this stored procedure with Entity Framework without it being in a transaction?
Pass TransactionalBehavior.DoNotEnsureTransaction as the first parameter to the ExecuteSqlCommand method.
For example,
this.Context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "EXEC edi_UploadTransmission");
My recommendation would be to simply not use EF for this part of your code. You can freely combine EF with straight ADO.NET code or with other ORMs such as Dapper or Chain.
https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper
I need to configure the database created by Entity Framework Code First MigrateDatabaseToLatestVersion class.
Is it possible to influence the database files parameters like size or maxsize? What interests me in particular, is there a way to add database files?
I assume I need to find the moment where the Entity Framework generates the CREATE DATABASE statement and influence it a bit. As far as I understand, the SqlServerMigrationSqlGenerator class is too late because the database already exists at this point.
Edit: According to comment this doesn't work:
You can just add code based migration using Add-Migration command and modify its Up method to execute Sql("ALTER DATABASE ...") and do what ever you want with your database.
Database is created through DbProviderServices - that is a bridging component between EF and specific database server. ObjectContext exposes operations for creating database and generating database creation script. DbMigrator use database creation operation when you execute Update. It checks that if Database exists and if not it uses ObjectContext.CreateDatabase. So if you want to change the process of creating the database you need to implement your own implementation of the migrator to change the way how Update method generates the database (maybe you just need a decorator which will create a database prior to executing DbMigrator.Update operation).
I have DocumentItem entity mapped to insert/update/delete stored procedures in Entity Framework edmx.
I'm trying to insert a new Document into the databse along with its DocumentItems. The whole operation is enclosed in a transaction, and it's not easy to debug separately.
This is why I would like to try to debug the sp 'live' - when it's called from entity framework. Is it possible at all?
Just use profiler to see what data EF sends to stored procedure and use that data separately to test / debug only stored procedure. Debugging it together requires you to set debugging session for both .NET code and SQL code and place breakpoint into stored procedure prior to calling SaveChanges on your context. In theory it could work but I never use that.