entity-framework-core

EFCore - Seed data only if it doesn't exist in another table

痴心易碎 提交于 2020-08-10 19:03:06
问题 I want to seed some data using EntityFramework in .net core 3.1 and I'm facing an issue: I've got two SQL tables (so two DbSet<>): public virtual DbSet<TableA> TableA { get; set; } public virtual DbSet<TableB> TableB { get; set; } Table A has this structure: [Key] public int Id { get; set; } // PK public string EnglishText { get; set; } // some value Table B has this structure: [Key] public int Id { get; set; } // PK public int TableAId { get; set; } // FK to Table A public string

Error NU1107 Version conflict detected for Microsoft.EntityFrameworkCore. Install/reference while installing EF core

房东的猫 提交于 2020-08-10 04:47:07
问题 I installed a visual studio 2017 and I have created a MVC project called WebApplication1. In the Dependencies-> NuGet I have the files Microsoft.AspNetCore.App(2.2.0) Microsoft.AspNetCore.Razor.Design(2.2.0). In Dependencies->SDK: Microsoft.AspNetCore.App(2.2.0) Microsoft.NETCore.App (2.2.0) I want to install Entity Framework Core for my project. Thus I right click on my project and select "Manage Nuget Packages" and then in brows section I enter "Microsoft.EntityFrameworkCore.SqlServer" in

How to change registered (Simple Injector) DbContext's connection string after user's authorization?

余生长醉 提交于 2020-08-09 08:14:43
问题 I'm building ASP.NET Core Web Api with Simple Injector and have the following problem: The data for each user is stored in his individual base (but with the same schema), which is known only after he authorizes himself. So... I can give DbContext it's proper connection string only after user's authorization. What is the best way to accomplish this? For now I am storing the connection string in HttpContext custom claim and am refering to it with a use of a static helper class in DbContext 's

How to change registered (Simple Injector) DbContext's connection string after user's authorization?

巧了我就是萌 提交于 2020-08-09 08:13:54
问题 I'm building ASP.NET Core Web Api with Simple Injector and have the following problem: The data for each user is stored in his individual base (but with the same schema), which is known only after he authorizes himself. So... I can give DbContext it's proper connection string only after user's authorization. What is the best way to accomplish this? For now I am storing the connection string in HttpContext custom claim and am refering to it with a use of a static helper class in DbContext 's

Why EF Core 2.2.6 does not garbage collect?

可紊 提交于 2020-08-08 05:31:38
问题 I am using the dotMemoryUnit to prove the my DbContext object is getting garbage collected properly. I feel that this code should work properly in a unit test, but the test always fails. The only thing I can guess is EF Core is holding a reference somewhere. Edit: I'm not sure the suggested question addresses this problem as this is .Net Core and not .Net Framework. The documentation for GC.Collect()'s default is forced and the documentation says nothing about hints. Edit 2: I did find the

.Net EF Core 2.1 Get DbContext from IQueryable argument

爱⌒轻易说出口 提交于 2020-08-07 15:12:23
问题 I have an IQueryable extension method: public static void SomeExt<T>(this IQueryable<T> query, DbContext context) {...} and I would like to know if there is some way to get DbContext from query so that DbContext argument could be removed leaving only: public static void SomeExt<T>(this IQueryable<T> query) {...} I have tried something like this Access DataContext behind IQueryable but its not working, getting zero fields. Also there is way to get it from DbSet Can you get the DbContext from a

How can I create a Required Owned Type with Entity Framework Core 3.0

陌路散爱 提交于 2020-08-07 08:03:50
问题 I'm struggling creating a non-nullable/required Owned Type with Entity Framework Core. I'm using EF Core 3.0 against PostgreSQL database. My value object: public class PersonName { public PersonName(string name) { this.Name = name; } public string Name { get; set; } } My entity: public class Person { public int Id { get; set; } public virtual PersonName FullName { get; set; } } My entity configuration: public void Configure(EntityTypeBuilder<Person> builder) { builder.ToTable(nameof(Person));

How can I create a Required Owned Type with Entity Framework Core 3.0

泪湿孤枕 提交于 2020-08-07 08:03:11
问题 I'm struggling creating a non-nullable/required Owned Type with Entity Framework Core. I'm using EF Core 3.0 against PostgreSQL database. My value object: public class PersonName { public PersonName(string name) { this.Name = name; } public string Name { get; set; } } My entity: public class Person { public int Id { get; set; } public virtual PersonName FullName { get; set; } } My entity configuration: public void Configure(EntityTypeBuilder<Person> builder) { builder.ToTable(nameof(Person));

Same DbContext, Several databases using EF Core

一曲冷凌霜 提交于 2020-08-06 05:54:57
问题 In my App, I have some data which are stored on different databases (e.g. identity and User Info are stored separately to Content-related stuff for historical reasons). However I would like to be able to get advantage of the '.Include' method of EF Core, but as the data is stored in a different DB, is there a way to query two databases in the same DbContext? 回答1: One option I am seeing is to use views to get all the information from the 2nd database back to the local database. If using SQL

Optimize EF Core query with Include()

南笙酒味 提交于 2020-08-05 09:25:01
问题 I have following query within my project and it is consuming lot of time to execute. I am trying to optimize it, but not able to successfully do it. Any suggestions would be highly appreciated. _context.MainTable .Include(mt => mt.ChildTable1) .Include(mt => mt.ChildTable1.ChildTable2) .Include(mt => mt.ChildTable3) .Include(mt => mt.ChildTable3.ChildTable4) .SingleOrDefault( mt => mt.ChildTable3.ChildTable4.Id == id && mt.ChildTable1.Operation == operation && mt.ChildTable1.Method = method &