Showing posts with label Entity Framework Core. Show all posts
Showing posts with label Entity Framework Core. Show all posts

Saturday, August 8, 2020

EntityFramework Core : Transaction execution issue when using In-Memory database

The EntityFramework In-Memory database does not support Transactions as of this writing. Therefore you might get the error: 

Error generated for warning 'Microsoft.EntityFrameworkCore.Database.Transaction.TransactionIgnoredWarning: Transactions are not supported by the in-memory store. See http://go.microsoft.com/fwlink/?LinkId=800142'. This exception can be suppressed or logged by passing event ID 'InMemoryEventId.TransactionIgnoredWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.


To solve this issue, you need to ignore the Transaction Warning when EF is Configured.

 iWebHostBuilder.ConfigureServices(services =>
{
                   services.AddDbContext<IvitDbContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDb");
                    //In memory db does not support transactions. Hence ignore transaction warnings
                    options.ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                });
});


Friday, February 7, 2020

Entitty Framework Core - Migrations

To migrate through the package manager console download the Nuget "Microsoft.EntityFrameworkCore.Tools"

The Enable-migration is deprecated in Core, so no need of this command as the migrations are enabled by default.


Adding a migration
Example : Add-Migration MyFirstMigration

Update the database
update-database

To get a SQL script containing all migration snapshots use the below command:
 Script-Migration

For more help on EF Core migrations type in the below command in Package manage console
 Get-Help entityframework