EF Core 2: How to apply HasQueryFilter for all entities

In case you have base class or interface defining the IsActive property, you could use the approach from Filter all queries (trying to achieve soft delete). Otherwise you could iterate entity types, and for each type having bool IsActive property build dynamically filter expression using Expression class methods: foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { var … Read more

Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists

There is no -IgnoreChanges currently in EF Core (see here) but you can achieve the equivalent by commenting out all the code in the Up() method and applying the migration. This will take a snapshot of the current model state so that subsequent migrations will only include changes from that point forward. So if you … Read more

DbFunctions.TruncateTime LINQ equivalent in EF CORE

In EF6 DbFunctions.TruncateTime is used instead of DateTime.Date property because for some reason the later is not supported. In EF Core the former is not needed simply because DateTime.Date now is recognized and translated correctly. group events by events.DateTimeFrom.Date into dateGroup Unfortunately there is no documentation (yet) of what is supported, so as a general … Read more

How to resolve .net core build error NETDSDK1061 and warning MSB3277

I’ve tried to find any help on that issue, finding some GitHub issues (e.g. this one) looking pretty similar, but were actually different. I’ve found a descriptive doc, but that didn’t really helped me. I found a pretty helpful blog post from Rick Strahl, explaining what packages are available and what the purpose of each … Read more

Entity Framework Core 2.0 – Run migrations step by step

You can use the GetPendingMigrations extension method of the DatabaseFacade class (returned by Database property of the DbContext) to get the list of the pending migration names. Then you can obtain IMigrator service and use Migrate method passing each target migration name: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; DbContext db = …; var pendingMigrations = … Read more

Run database migrations using Entity Framework core on application start

You can do this in the config methods in your Startup.cs. The simplest way is like this: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(); // add other services } public void Configure(IApplicationBuilder app, ApplicationDbContext db) { db.Database.Migrate(); // configure other services }

EF Core takes a lot of time, sometimes, to perform SELECT query

It is not new question when SQL Server may slowdown queries because of Parameter Sniffing. Problem can be solved by converting parameters to constants or by adding OPTION(RECOMPILE) to the end of the query. This answer adds DbCommandInterceptor to DbContextOptions and appends OPTION(RECOMPILE) hint to particular queries. Configuring DbContext builder.UseSqlServer(connectionString) .UseRecompileExtensions(); // registering interceptor How … Read more

How to set Identity Seed value in code-first?

Update 2020. After EF Core 3.0 now you have a UseIdentityColumn extension method which can be used for setting the seed and increment values for identity columns. builder.Property(prop => prop.Id) .UseIdentityColumn(10000000, 1); As per offcial documentation: UseIdentityColumn Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities, when … Read more