entity-framework-core

Entity Framework One to many relationship

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:52:36
问题 My EF query takes around 3 seconds to fetch 10 players because it fetches all 500k+ rows of the other table, instead of the few I need. This is the PlayerEntity: namespace RocketLeagueStats.Database.Entities { [Table("players", Schema = "public")] public class PlayerEntity { [Key] [Column("id")] public int Id { get; set; } [Column("unique_id")] public string UniqueId { get; set; } [Column("display_name")] public string DiplayName { get; set; } [Column("platform_id")] [JsonIgnore] public int

Creating a DbContextFactory that gets the connection string from user-secrets

点点圈 提交于 2019-12-10 15:43:01
问题 Working on a DotNetCore solution with a WebApi project and a separate Data project that houses the Entity Framework implementation. We've been upgrading libraries as they come out, so we're using all of the newest Core stuff. In the Data project, we created an ApplicationDbContextFactory in order to create migrations (needs a parameterless constructor). Due to the parameterless constructor constraint when adding a migration, you can't inject IOptions<> to easily access appsettings.json values

Recommended way to clean old Entity Framework Core migrations

若如初见. 提交于 2019-12-10 15:41:06
问题 After developing our application for a while we've accumulated quite a bit of EFCore database migrations. Since EFCore adds a snapshot of the entire db model to every migration, this code adds up quite a bit. After analysis about 80% of our compile time is spend on the migrations (compiling + Roslyn analyzers). So it's time to clean up some old migrations! But what's the best way to do this? There doesn't seem to be any official guidance on it... We don't need any rollbacks (we only roll

Using Singular Table Names with EF Core 2

天大地大妈咪最大 提交于 2019-12-10 14:55:45
问题 I want my domain class name to match my db table name (no pluralisation). In EF Core 1.1, I used this code to do that: public static void RemovePluralisingTableNameConvention(this ModelBuilder modelBuilder) { foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes()) { entityType.Relational().TableName = entityType.DisplayName(); } } In EF Core 2.0, this code doesn't compile as Relational() is not a method on IMutableEntityType. Anyway, in EF Core 2.0, they have added

ASP.NET Core won't run in IIS after upgrade to v1.1

六眼飞鱼酱① 提交于 2019-12-10 14:49:25
问题 I've upgraded my project.json to use the new v1.1 of asp.net core and now when I try to start IIS Express to debug, I get the following errors: The program '[8784] dotnet.exe' has exited with code -2147450749 (0x80008083). The program '[7352] iisexpress.exe' has exited with code 0 (0x0). My upgraded project.json: "dependencies": { "Microsoft.NETCore.App": { "version": "1.1.0", "type": "platform" }, "Microsoft.ApplicationInsights.AspNetCore": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.1.0",

How to do Multiple ThenInclude navigation props after One Include

吃可爱长大的小学妹 提交于 2019-12-10 14:25:13
问题 For a TestType I wanted to include both navigation props Schoolclass and Subject. I could do a: .Include(t => t.TestType) .ThenInclude(x => x.Subject) but not a: .Include(t => t.TestType) .ThenInclude(x => x.Subject) .ThenInclude(x => x.Schoolclass) Thus I tried a little trick and that worked: I included the TestType 2 times... var test = await context.Tests.Where(t => t.SchoolyearId == schoolyearId) .Include(t => t.TestType) .ThenInclude(x => x.Subject) .Include(t => t.TestType) .ThenInclude

EF Core Backing fields - expose property as another type?

二次信任 提交于 2019-12-10 14:08:56
问题 Assume I have an EF entity class Person, with a PhoneNumber on it. PhoneNumber is stored as a string type, but I want all accesses on the Person to go through Phone which has some nice accessor functions, e.g. validation or GetAreaCode() . I want to back it in the db as a string, but when queried for it I want to return it as a PhoneNumber: public class Person { public PhoneNumber Phone { /* Some clever get/set logic here */ } private string _phoneNumber; // Backing field } Or can I get

ToAsyncEnumerable().Single() vs SingleAsync()

大城市里の小女人 提交于 2019-12-10 13:46:59
问题 I'm constructing and executing my queries in a way that's independent of EF-Core, so I'm relying on IQueryable<T> to obtain the required level of abstraction. I'm replacing awaited SingleAsync() calls with awaited ToAsyncEnumerable().Single() calls. I'm also replacing ToListAsync() calls with ToAsyncEnumerable().ToList() calls. But I just happened upon the ToAsyncEnumerable() method so I'm unsure I'm using it correctly or not. To clarify which extension methods I'm referring to, they're

Toggling ProxyCreation in EF7 under new configuration

与世无争的帅哥 提交于 2019-12-10 13:27:38
问题 So in EF6, one could disable proxy creation like so: this.Configuration.ProxyCreationEnabled = false; From what I could find, the configuration scheme under EF7 has changed, but I cannot find anything on how to do so. I went through https://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html and even analyzed the DbContextOptionsBuilder object, but cannot find anything on it. Am I going about ti the wrong way or is there something im missing? Thanks in advance. 回答1: EF7 EF

Entity Framework Core 2.0 mapping enum to tinyint in SQL Server throws exception on query

泄露秘密 提交于 2019-12-10 13:15:58
问题 I get the following exception when I try to map an enum to smallint in OnModelCreating : InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'. I want to do this because in SQL Server an int is 4 bytes while a tinyint is 1 byte. Relevant code: Entity: namespace SOMapping.Data { public class Tag { public int Id { get; set; } public TagType TagType { get; set; } } public enum TagType { Foo, Bar } } DbContext: using Microsoft.AspNetCore.Identity