entity-framework-core

How to deal with database changes during development with EF Core?

旧城冷巷雨未停 提交于 2019-12-05 21:01:24
I am struggling with database changes during development with .NET Core and Entity Framework Core. When I create new model and add it to dbContext, even with dbContext.Database.EnsureCreated(); in Startup.cs it does not create new tables. It will create all tables only if I will do dotnet ef database drop before, but it is pretty annoying to have to seed db every time adding new model. creating more and more migration every time does not look like good idea to me... Can someone suggest better way? Unfortunately it seems that EF core does not support automatic migrations like in EF 6. This

EntityFrameworkCore: How to initialize a Database and seed it the first time user uses an application

守給你的承諾、 提交于 2019-12-05 20:55:35
I have build a project using Microsoft Visual Studio 2015 and EntityFrameworkCore. I have seed manually a couple of dummy data and I was developing my solution. Now, I want to deploy the in the server, but I get the problem that by starting the application the first time, it crash since it does not find a data base and data. I have googled and I find the solution for Visual Studio 2013 and previous using the CreateDatabaseIfNotExists class that need the package: System.Data.Entity ( http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx ), however

deleting some items from a child collection in ef core

半世苍凉 提交于 2019-12-05 20:01:47
I have a parent table and child table, where the parent has a one-to-many relationship with the children. I want to delete some of the children and I want the parent's child collection to reflect that change. If I delete the selected children using RemoveRange , then the child collection doesn't get updated. If I use Remove to remove the children from the child collection then (apparently) it's not as efficient as using RemoveRange . So I have to use RemoveRange to delete the children efficiently and then use Remove to remove them from the child collection. Is this correct or is there a better

EF Core self referencing not working if there are 2 foreign keys to self (Code first)

会有一股神秘感。 提交于 2019-12-05 19:20:45
If I defined (in code first) one navigation property to self it works (foreign key is created), but if I define 2 it doesn't work. How can I create 2 foreign keys to self? Based on documentation by conventions they should be created. For example this works (foreign key is created): public class DbPart { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [ForeignKey("Source")] public int? SourceId { get; set; } public DbPart Source { get; set; } [InverseProperty("Source")] public List<DbPart> SourceParts { get; set; } } and so is this: public

The child/dependent side could not be determined for the one-to-one relationship

你。 提交于 2019-12-05 18:10:14
I am trying to update my database with "update-database" command in package manager console, But I have this kind of error: The child/dependent side could not be determined for the one-to-one relationship between 'Country.CapitalCity' and 'CapitalCity.Country'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details. My Model classes look like this: public class Country { public

Avoid Entity Framework Error with Multiple Tasks Running Concurrently on Same DbContext

こ雲淡風輕ζ 提交于 2019-12-05 18:02:38
问题 I have a WebApi controller in a Dotnet Core project running Entity Framework Core with Sqlite. This code in an action occationally produces errors: var t1 = _dbContext.Awesome.FirstOrDefaultAsync(a => [...]); var t2 = _dbContext.Bazinga.FirstOrDefaultAsync(b => [...]); var r1 = await t1; var r2 = await t2; The errors have been: Microsoft.EntityFrameworkCore.Query.RelationalQueryCompilationContextFactory:Error: An exception occurred in the database while iterating the results of a query.

EF 7: How to load related entities in a One-to-many relationship

旧街凉风 提交于 2019-12-05 17:53:55
I have the following code. Why are my navigation properties (Requirement in Course, and Courses in Requirement) are null? public class Course : AbsEntity { [Key] public string Title { get; set; } public string Term { get; set; } public int Year { get; set; } public string CourseId { get; set; } public double GradePercent { get; set; } public string GradeLetter { get; set; } public string Status { get; set; } public int ReqId { get; set; } public Requirement Requirement { get; set; } } public class Requirement : AbsEntity { [Key] public int ReqId { get; set; } public string ReqName { get; set;

Could not load assembly Microsoft.EntityFrameworkCore.Design when running Add-Migration

旧巷老猫 提交于 2019-12-05 17:25:33
I have created a Class Library project with the following .csproj: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.sqlserver.Design" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" /> </ItemGroup> </Project> When running the command: Add

What is a proper way of writing entity POCO classes in Entity Framework Core?

馋奶兔 提交于 2019-12-05 17:23:21
问题 EF Core has a "code first mentality" by default, i.e. it is supposed to be used in a code-first manner, and even though database-first approach is supported, it is described as nothing more than reverse-engineering the existing database and creating code-first representation of it. What I mean is, the model (POCO classes) created in code "by hand" (code-first), and generated from the database (by Scaffold-DbContext command), should be identical. Surprisingly, official EF Core docs demonstrate

Workaround needed for EF Core performing GroupBy operations in memory instead of in SQL

纵然是瞬间 提交于 2019-12-05 16:14:41
I'm working in Entity Framework Core 1.1.0 (and upgrading is not an option at this point, due to breaking changes in later versions). My query is of the following form: var q = db.MyTable .GroupBy(t => new { t.Field1 }) .Select(g => new { g.Key.Field1, MaxField2 = g.Max(x => x.Field2) }) .ToList(); In test code this works well and returns the expected data. But when deployed to a real environment, with real data, it times out. Why? Well, I put a sniffer on the SQL server, and here's the actual SQL: SELECT [t].[Field1], [t].[Field2], [t].[Field3], [t].[Field4], [t].[Field5] FROM [dbo].[MyTable]