Add migration with different assembly

All EF commands have this check: if (targetAssembly != migrationsAssembly) throw MigrationsAssemblyMismatchError; targetAssembly = the target project you are operating on. On the command line, it is the project in the current working directory. In Package Manager Console, it is whatever project is selected in the drop down box on the top right of that … Read more

How do you show underlying SQL query in EF Core?

Update for .NET 6 and later: EF logging is enabled by default in development. Just add “Microsoft.EntityFrameworkCore.Database.Command”: “Information” to appsettings.Development.json so it’s only logged in dev mode. You typically don’t want to log every query in a production app. { “ConnectionStrings”: { “DefaultConnection”: “Server=(localdb)\\mssqllocaldb;Database=MyDB-2;Trusted_Connection=True;MultipleActiveResultSets=true” }, “Logging”: { “LogLevel”: { “Default”: “Information”, “Microsoft”: “Warning”, “Microsoft.Hosting.Lifetime”: “Information” … Read more

Unable to create migrations after upgrading to ASP.NET Core 2.0

You can add a class that implements IDesignTimeDbContextFactory inside of your Web project. Here is the sample code: public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<CodingBlastDbContext> { public CodingBlastDbContext CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(“appsettings.json”) .Build(); var builder = new DbContextOptionsBuilder<CodingBlastDbContext>(); var connectionString = configuration.GetConnectionString(“DefaultConnection”); builder.UseSqlServer(connectionString); return new CodingBlastDbContext(builder.Options); } } Then, navigate to … Read more

Filtering on Include in EF Core

Entity Framework core 5 is the first EF version to support filtered Include. How it works Supported operations: Where OrderBy(Descending)/ThenBy(Descending) Skip Take Some usage examples (from the original feature request and the github commmit) : Only one filter allowed per navigation, so for cases where the same navigation needs to be included multiple times (e.g. … Read more

How and where to call Database.EnsureCreated and Database.Migrate?

I think this is an important question and should be well answered! What is Database.EnsureCreated? context.Database.EnsureCreated() is new EF core method which ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created and also it ensures … Read more

How to update record using Entity Framework Core?

To update an entity with Entity Framework Core, this is the logical process: Create instance for DbContext class Retrieve entity by key Make changes on entity’s properties Save changes Update() method in DbContext: Begins tracking the given entity in the Modified state such that it will be updated in the database when SaveChanges() is called. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)