EF Core returns null relations until direct access

The reason is explained in the Loading Related Data section of the EF Core documentation. The first behavior is because EF Core currently does not support lazy loading, so normally you’ll get null for navigation properties until you specifically load them via eager or explicit loading. However, the Eager loading section contains the following: Tip … Read more

Asp.Net Core – The configured user limit (128) on the number of inotify instances has been reached

The best solution I found so far is to increase the fs.inotify.max_user_instances in /etc/sysctl.conf by running this command: echo fs.inotify.max_user_instances=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p Source: https://github.com/dotnet/aspnetcore/issues/8449#issuecomment-512275929

How to set command timeout in ASP.NET Core / Entity Framework Core?

If you’re using the DI container to manage the DbContext (i.e. you’re adding the DbContext to the service collection), the command timeout can be specified in the options. In Startup.ConfigureServices: services.AddDbContext<YourDbContext>(options => options.UseSqlServer( this.Configuration.GetConnectionString(“YourConnectionString”), sqlServerOptions => sqlServerOptions.CommandTimeout(60)) );

How to use C# 9 records with EF Core?

From official document Entity Framework Core depends on reference equality to ensure that it uses only one instance of an entity type for what is conceptually one entity. For this reason, record types aren’t appropriate for use as entity types in Entity Framework Core. This could confuse some people. Pay close attention to the documentation. … Read more

EntityFramework code first: Set order of fields

Currently ordering columns by class property is not implemented. Here’s the long discussion about column ordering. Column ordering #2272 Update as of 07/12/2017 This issue is in the Backlog milestone. This means that it is not going to happen for the 2.0 release. We will re-assess the backlog following the 2.0 release and consider this … Read more

DbContext for background tasks via Dependency Injection

You should pass IServiceScopeFactory instance (it’s singleton) into your task. Inside task, when data arrives, you should create new CreateScope() and request services from that scope. When data process finishes – dispose this scope (but hold reference to IServiceScopeFactory for next run). See this for example. I run small and fast tasks with this library. … Read more

ASP.NET Core & EntityFramework Core: Left (Outer) Join in Linq

If you need to do the Left joins then you have to use into and DefaultIfEmpty() as shown below. var result = from person in _dbContext.Person join detail in _dbContext.PersonDetails on person.Id equals detail.PersonId into Details from m in Details.DefaultIfEmpty() select new { id = person.Id, firstname = person.Firstname, lastname = person.Lastname, detailText = m.DetailText … Read more

Seed entity with owned property

Currently this information is missing from the documentation (tracked by #710: Document how to seed owned types). It’s explained by EF Core team (with example) in the #12004: Problem seeding data that contains owned type thread: Owned types must be seeded with a HasData call after the OwnsOne call. Also, since owned types by convention … Read more

Execute SQL command in Entity Framework Core 2.0 to delete all data in a table

Ensure that you reference Microsoft.EntityFrameworkCore to include all the necessary extension methods that would allow you to execute raw SQL commands. From the source repository I found ExecuteSqlCommand and related extension methods int count = await context.Database.ExecuteSqlCommandAsync(“DELETE FROM [Blogs]”); Found an article that suggested using ADO.Net. First you grab a connection from the context, create … Read more

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