Include several references on the second level

.ThenInclude() will chain off of either the last .ThenInclude() or the last .Include() (whichever is more recent) to pull in multiple levels. To include multiple siblings at the same level, just use another .Include() chain. Formatting the code right can drastically improve readability. _dbSet .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.Titre) .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.TypeContact) .Include(tiers … Read more

Can I generate script of a migration with EF code first and .net core

As per EF documentation you can use Script-Migration command. If you want to just script all the migrations you can simply call it from Package Manager console like that. If you want to just script the changes from the last migration you can call it like this: Script-Migration -From <PreviousMigration> -To <LastMigration> Be sure to … Read more

EF Core Mapping EntityTypeConfiguration

Since EF Core 2.0 there is IEntityTypeConfiguration<TEntity>. You can use it like this: class CustomerConfiguration : IEntityTypeConfiguration<Customer> { public void Configure(EntityTypeBuilder<Customer> builder) { builder.HasKey(c => c.AlternateKey); builder.Property(c => c.Name).HasMaxLength(200); } } … // OnModelCreating builder.ApplyConfiguration(new CustomerConfiguration()); More on this and other new features introduced in 2.0 can be found here.

How to auto create database on first run?

If you have created the migrations, you could execute them in the Startup.cs as follows. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); context.Database.Migrate(); } … This will create the database and the tables using your added migrations. If you’re not using Entity Framework … Read more

.Net Core 3.0 possible object cycle was detected which is not supported

I have tried your code in a new project and the second way seems to work well after installing the package Microsoft.AspNetCore.Mvc.NewtonsoftJson firstly for 3.0 services.AddControllersWithViews() .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore ); Try with a new project and compare the differences.

Get SQL code from an Entity Framework Core IQueryable

EF core 5/6 / Net 5/6 query.ToQueryString() See Documentation ToQueryString() and What’s New in EF Core 5.0 var query = _context.Widgets.Where(w => w.IsReal && w.Id == 42); var sql = query.ToQueryString(); For older net core frameworks an Extension can be used. Core 2.1.2 using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Query.Internal; using Microsoft.EntityFrameworkCore.Query.Expressions; using Microsoft.EntityFrameworkCore.Query.Sql; … Read more

The term “Add-Migration” is not recognized

Just install Microsoft.EntityFrameworkCore.Tools package from nuget: Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.8 You can also use this link to install the latest version: NuGet package link .NET CLI command: dotnet add package Microsoft.EntityFrameworkCore.Tools If the problem still persists, try restarting Visual Studio.