entity-framework-core

One-to-many relationship getting error in Entity Framework Core when trying to insert data

自闭症网瘾萝莉.ら 提交于 2019-12-07 07:46:19
I'm trying to add data to one-to-many relationship tables. But there is an exception. There are two tables: Post Attachment The Post has many Attachment but one Attachment has one unique post. I'm going to try: Adding records to the Post table, then after that using that post-Id. update the Attachment table Here is the exception thrown InvalidOperationException: The property 'Id' on entity type 'Posts' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property. at Microsoft.EntityFrameworkCore.Update.Internal

.NET Core Entity Framework stored procedures

偶尔善良 提交于 2019-12-07 06:32:07
问题 I'm trying to port a ASP.NET 4.5 application to .NET Core and I have one real issue I can't seem to figure out. My existing application executes stored procs which return a dataset with multiple datatables. The Entity Framework can automatically map the returned fields to my entity properties but only works with the first datatable in the dataset (naturally). So I'm just trying to figure out if its at all possible to somehow intercept the model building process and use custom code to process

How does EF Core Modified Entity State behave?

笑着哭i 提交于 2019-12-07 06:28:43
问题 Does it matter we put the entity state = modified after changes or before making changes? using (var db = new LakshyaContext()) { foreach (var category in db.Categories) { db.Entry(category).State = EntityState.Modified; // before category.Count = 25; //Making Changes db.Entry(category).State = EntityState.Modified; //After } db.SaveChanges(); } 回答1: So first, let's get the most important thing out of the way: You are right. In your example, you don't need to manually call db.Entry(category)

Linq select distinct count performed in memory

五迷三道 提交于 2019-12-07 06:12:23
问题 I'm working on understanding how LINQ converts to SQL. I have the following query I'm trying to generate using LINQ. SELECT [OrganizationId] ,[DepartmentId] ,[LocationName] ,[Group1] ,[Group2] ,[Group3] ,[BooklistId] ,[BooklistName] ,COUNT(DISTINCT [OrdererId]) ,MAX([ExpectedDate]) FROM [Orders] WHERE ([DepartmentId] IS NOT NULL AND ([DepartmentId] = '<Guid>')) AND ([Schoolyear] = '2018') GROUP BY [OrganizationId] ,[DepartmentId] ,[LocationName] ,[Group1] ,[Group2] ,[Group3] ,[BooklistId] ,

Scaffolding in .Net Core with Multiple Projects in Solution

痞子三分冷 提交于 2019-12-07 06:05:48
问题 I have created an .Net Core MVC6 application targeting net461. I have used a project structure I am very familiar with in which I place the data, model, and service classes in separate class library projects and the Web project references these. When I attempt to scaffold a controller I receive an error that multiple matching types exist for the model I am scaffolding. If I move all code to a single project, scaffolding is successful. If I move the context to Web project and leave the model

Add-Migration Error in Entity Framework Core

假装没事ソ 提交于 2019-12-07 05:42:10
问题 I am following Julie Lerman's Getting Started with Entity Framework Core 2.0 tutorial. I have installed Microsoft.EntityFrameworkCore.SqlServer Version 2.0.2. When I try to run add-migration initial (Package Manager Console) in VS 2017 I am obtaining the below error : System.IO.FileLoadException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The located assembly's manifest definition does not match

Redirect Login to Controller Action

妖精的绣舞 提交于 2019-12-07 05:02:37
问题 Starting with the ASP.NET 5 Web App Template using Individual User Accounts I have managed to get external authentication working with Microsoft accounts. When users click Login they are redirected to ExternalLogin in AccountController like this <form asp-controller="Account" asp-action="ExternalLogin" method="post" asp-route-returnurl="@ViewData["ReturnUrl"]" class="nav navbar-right"> <button type="submit" class="btn btn-null nav navbar-nav navbar-right" name="provider" value="Microsoft"

EF Core fluent mapping to inner object properties

核能气质少年 提交于 2019-12-07 04:38:23
问题 I have a class that contains some properties. For some architectural reasons, I have an instance of another objet into my class. Simple example public class MyEntity { public MySubEntity SubEntity {get; set;} } For this, I create fluent mapping like : builder.ToTable(MyEntity.CONST_TABLE_NAME); builder.HasKey(m => m.Id); builder.Property(m => m.Column1).IsRequired(); builder.Property(m => m.SubEntity.Column2).IsRequired(); I cannot integrate all my subEntity properties into my main entity (my

Protect SQLite database used by EntityFramework Core Application

↘锁芯ラ 提交于 2019-12-07 04:08:30
问题 I'm trying to use EntityFramework Core 1.0 (new name for EntityFramework 7) to manage an SQLite database in a C# Desktop application. I read official documentation and my test-application works. My question is: is it possibile to password-protect the database? I know that there are lot of way to do it, it exists also the .NET class System.Data.SQLite that allow to do that, but how to do it using EFCore 1? I can't understand if it is possible. Thanks for help, Enrico 回答1: With EntityFramework

WithOptional with Entity Framework Core

感情迁移 提交于 2019-12-07 03:46:28
问题 I'm trying to migrate my old app to the new EF Core but I cannot find some relationships like: HasRequired(o => o.Document).WithOptional(o => o.CancelNote); Is there some extension methods? I cannot find on the docs. The HasRequired I think that is possible to substitute with HasOne() method, but how about the WithOptional() ? Other thing, according to the docs the entity not uses the virtual keyword to create the navigation properties, how lazy load will work? 回答1: You will not find an