entity-framework-core

How can I achieve SQL CASE statement from LINQ

不羁岁月 提交于 2019-12-24 00:34:28
问题 I am new to LINQ and I would like to know if I can achieve the below SQL query from LINQ? I am using Entity Framework Core. SELECT 0 [All], [Range] = CASE WHEN Value BETWEEN 0 AND 25 THEN 'Low' WHEN Value BETWEEN 25 AND 75 THEN 'Medium' WHEN Value BETWEEN 75 AND 90 THEN 'High' WHEN Value BETWEEN 90 AND 100 THEN 'Very High' END FROM Result.Calculation C INNER JOIN Data.SampleSet S ON C.SampleSetID = S.ID WHERE S.SampleDrawn >= DATEADD(MONTH,-3,GETDATE()) AND S.Department = 'LOCATION A'

Scaffold-DbContext not generating virtual navigation properties (EF Core 2.1)

不打扰是莪最后的温柔 提交于 2019-12-24 00:05:29
问题 I am using EF Core 2.1 database-first. I use Scaffold-DbContext to create/update my models (the .cs files) I want to enable lazy-loading, and I have added .UseLazyLoadingProxies() in my Startup.cs Now, how do I get Scaffold-DbContext to generate virtual navigation properties? 来源: https://stackoverflow.com/questions/50827976/scaffold-dbcontext-not-generating-virtual-navigation-properties-ef-core-2-1

EF Core Migration error: Database 'MyDatabaseName' already exists. Choose a different database name

独自空忆成欢 提交于 2019-12-23 19:43:05
问题 I am running ASP.NET Core 2.1 with EF Core 2.1 application on Windows Server 2016 with SQL Server 2017 Web edition. At the end of public void Configure(IApplicationBuilder app, ... method in Startup.cs I call context.Database.Migrate(); . This applies migrations. Everything works. Now I backup database in my development environment in SQL Server 2016, move MyDatabaseName .bak file to server and restore database MyDatabaseName on server and restart IIS site. When I start application (open

Update of navigation property in EF7 does not get saved

删除回忆录丶 提交于 2019-12-23 18:01:06
问题 I have simple model: public class Post { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { set; get; } //non-important properties stripped, to focus on problem public virtual Resource Resource { set; get; } public virtual ICollection<Tag> Tags { set; get; } } public class Resource { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { set; get; } [Url] public string Url { set; get; } } and DbContext (I use ASP.NET identity in this project, if

Get output parameter value of a stored procedure using EF Core?

对着背影说爱祢 提交于 2019-12-23 16:31:05
问题 I am using Asp.net core and EF core in my application. Basically I want to get multiple result set from a single stored procedure. Tried to search it for last 2 days no such luck. Tried to figure out a work around to resolve it.. This is my stored procedure: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[usp_CustomerAll_sel] @SomeOutput int OUT AS BEGIN SET NOCOUNT ON; SELECT * FROM [dbo].[Customer] SELECT @SomeOutput = @@rowcount + 25 --This number 25 is a variable

EF Core: No connection string named 'MyEntities' could be found in the application config file

冷暖自知 提交于 2019-12-23 16:00:30
问题 I have a .NET 5 Web Api Project(VS 2015) used some project references(VS 2013) using entity framework 6.1.3. When run the Web Api Project, I have got the exception "No connection string named 'MyEntities' could be found in the application config file". this is my config: { "webroot": "wwwroot", "version": "1.0.0-*", "dependencies": { "Microsoft.AspNet.Server.IIS": "1.0.0-beta6", "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5", "EntityFramework": "6.1.3", "structuremap": "3.1.6.186",

Getting Invalid column name 'EmploymentTypeEntityEmploymentTypeID in Entity framework core

佐手、 提交于 2019-12-23 14:55:51
问题 I am getting the below error. Message: Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details. ----> System.Data.SqlClient.SqlException : Invalid column name 'EmploymentTypeEntityEmploymentTypeID'. Its strange as its combining my Entity Class Name and the Entity Property Name. Below is my code. SystemTest.cs using (var transaction = _referenceDataDbContext.Database.BeginTransaction()) { _referenceDataDbContext

EF Core 2.1 HasData() creating deletes and re-inserts for unchanged entities on subsequent migrations

好久不见. 提交于 2019-12-23 14:38:08
问题 Update 8/29/18 Seeing this issue in inline new -ing of seed data as well. Opened an EF Core issue. Will update the question with any findings. I am trying to use EF Core 2.1's seeding mechanism. However, I want to load the seed data from json flat files rather than hard-coding new C# objects. I wrote the extension method below, which worked well for the initial migration. It looks for files named [MyEntity].json in DataPath and deserializes them into objects. The problem is that if I add a

Entity framework core: How to test navigation propery loading when use in-memory datastore

旧街凉风 提交于 2019-12-23 13:24:31
问题 There is an interesting feature exists in entity framework core: Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded. This is nice in some cases. However at the current moment I'm trying to modelling many-to-many relation with

EF7 Identity not loading User extended properties

前提是你 提交于 2019-12-23 13:21:05
问题 I have an extended IdentityUser class which contains a reference to another entity on my DB, but whenever I try to get the User with the UserManager, the referenced entity always comes empty: My implementation of the User class public class Usuario : IdentityUser { public int ClienteID { get; set; } public virtual Cliente Cliente { get; set; } } A controller that uses the referenced property on the user [Authorize] [HttpGet] public async Task<Direccion> GET() { var usuario = await UserManager