entity-framework-core

Unable to call stored procedure with FromSql in EF Core

谁都会走 提交于 2019-12-11 08:48:30
问题 I am trying to call a stored procedure from EF Core, and am running into an error I don't know how to solve. Class: public class PlayerOverview { public string Player { get; set; } public int World { get; set; } public string Alliance { get; set; } public int Score { get; set; } public int Castles { get; set; } public int Cities { get; set; } } Query: _context.Set<PlayerOverview>() .FromSql($"CALL usp_player_overview('{player}')") .Select(pl => new { Player = pl.Player, World = pl.World,

Saving dependent record without creating a duplicate principal record

筅森魡賤 提交于 2019-12-11 08:46:10
问题 With EF Core I am creating a principal record, but when adding and saving a dependent record it will attempt to create a new duplicate principal. I've seen various solutions to this, but I'd like to know why this is happening to make a proper fix. The entity models are in this fashion. public class Company { public string ID { get; set; } public string Name { get; set; } } public class Employee { public string ID { get; set; } public string Title { get; set; } public string Name { get; set; }

Determine if property is a Navigation Property in EF Core

自闭症网瘾萝莉.ら 提交于 2019-12-11 08:43:32
问题 I'm building a simple change tracker to capture all the edits to a Sql Azure database (unfortunately, Sql Azure doesn't support this natively, so far as I can tell). I'm walking the list of modified entries returned by ChangeTracker(): foreach( EntityEntry entry in _context.ChangeTracker.Entries() .Where( e => e.State == EntityState.Modified ) ) { foreach( var prop in entry.Entity .GetType() .GetTypeInfo() .DeclaredProperties ) { // this line blows up on navigation properties PropertyEntry

Validate that EF Core ModelSnapshot, migrations, and actual database schema are consistent with each other

会有一股神秘感。 提交于 2019-12-11 08:26:46
问题 I am using EF Core. After "cleverly" merging a couple of source control branches that both introduced migrations, I'm no longer confident that my ModelSnapshot is consistent with either my migrations or my actual database. Is there some command or function that I can use to validate that the three are consistent with each other (i.e. that the snapshot is a faithful representation of the current database schema and that the migrations, when applied to a newly-created database, will generate

How to get appsettings from project root to IDesignTimeDbContextFactory implementation in ASP.NET Core 2.0 and EF Core 2.0

删除回忆录丶 提交于 2019-12-11 08:07:25
问题 I am building an application in ASP.NET Core 2.0 and I am having problems with EntityFramework Migrations. I have my DbContext in a separate project ( SolutionName \ ProjectNamePrefix .Data) and therefore I created an implementation for the IDesignTimeDbContextFactory interface. I wanted to use different connection strings for different environments and I need appsettings.json for that. So after a quick search I found that I can create a new IConfigurationRoot object inside the

Setting SET IDENTITY_INSERT for multiple tables in Entity Framework Core

独自空忆成欢 提交于 2019-12-11 08:05:47
问题 I want to set tables IDENTITY_INSERT to ON . I can for one table at a time. But how can I achieve for more than one as I am doing code-first approach. I'm getting this error: System.Data.SqlClient.SqlException : IDENTITY_INSERT is already ON for table 'Some Table'. Cannot perform SET operation for table 'ref.EmploymentType' Test.cs using (var transaction = _referenceDataDbContext.Database.BeginTransaction()) { _referenceDataDbContext.EmploymentType.AddRangeAsync( new EmploymentTypeEntity {

EF Core 2.1 Props inside where cause null reference

删除回忆录丶 提交于 2019-12-11 07:33:16
问题 When I migrated from EF 1.1 to 2.1 I started getting null reference error when iterating inside where condition, it seems like StatusCashAdvanceList is not loaded at the time where is executed. CashAdvance.cs: public virtual ICollection<StatusCashAdvance> StatusCashAdvanceList { get; set; } Stack Trace: NullReferenceException: Object reference not set to an instance of an object. lambda_method(Closure , CashAdvance) System.Linq.Enumerable+WhereSelectEnumerableIterator<TSource, TResult>

automapper unflatten excludes value from parent object

試著忘記壹切 提交于 2019-12-11 07:06:24
问题 I've a situation where entity framework core (2.0) is performing additional work to a parent table when I update a row in a child table. I've pin-pointed the cause to a value not being set in the unflattened object tree produced by AutoMapper (I'm not saying it is an error in AutoMapper; it's probably more to do with my code). I'm using ASP.NET Core 2.0, C#, EF Core 2.0 and AutoMapper for the API development side. The database already exists and the EF classes scaffolded from it. To keep it

EF Core query stored procedure map to types

断了今生、忘了曾经 提交于 2019-12-11 07:05:21
问题 I have a project that needs to query a database and return results to web api. There are several stored procedures that are created on the fly by the DB Admin and they have a UI that they create the definition of the stored procedure and the name of it and the web api service just calls that SP and should return the result. Based on the below code I cannot get the return object to web api controller as object contains items but with no properties mapped. var result = dbContext.Query<object>()

Include not working with join entities

允我心安 提交于 2019-12-11 06:57:17
问题 As example I have following entities (many-to-many, I also removed unnessecary props): public class Buffet { public int Id {get; set;} public string Name {get; set;} } public class Recipe { public int Id {get; set;} public string Name {get; set;} public int CategoryId {get; set;} public virtual Category Category {get; set;} } public class Category { public int Id {get; set;} public string Name {get; set;} } Join entity: public class BuffetRecipe { public int BuffetId {get; set;} public