entity-framework

Linq Query with a Where clause in an Include statement

天涯浪子 提交于 2020-04-12 18:56:28
问题 I am trying to replace my big, ugly query; although ugly it works as desired:- using (var ctx = new Data.Model.xxxTrackingEntities()) { var result = ctx.Offenders .Join(ctx.Fees, o => o.OffenderId, f => f.OffenderId, (o, f) => new { Offenders = o, Fees = f }) .Join(ctx.ViolationOffenders, o => o.Fees.ViolationId, vo => vo.ViolationId, (o, vo) => new { Offenders = o, ViolationOffenders = vo }) .Join(ctx.Violations, v => v.ViolationOffenders.ViolationId, vo => vo.ViolationId, (v, vo) => new {

Stack overflow when adding an entity with multiple one-to-one relationship with the same table in Entity Framework Core

蹲街弑〆低调 提交于 2020-04-12 07:27:07
问题 I have an issue when adding entities with multiple one-to-one relationship with the same table in Entity Framework Core. Based on this question I have the following things: public class Article { public int Id { get; set; } public int? PreviousId { get; set; } public Article Previous { get; set; } public int? NextId { get; set; } public Article Next { get; set; } } In the OnModelCreating of the DbContext as I have: protected override void OnModelCreating(ModelBuilder modelBuilder) { base

Cannot update database because entity cannot drop constraint

柔情痞子 提交于 2020-04-11 08:29:10
问题 I created some new tables and I noticed I didn't use the good naming convention for the ids. So I did a new add-migration with the new ids but when I try to update-database I have the following error : The constraint 'PK_dbo.DB_User_Type' is being referenced by table 'AspNetUsers', foreign key constraint 'FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_Type_Id'. Could not drop constraint. See previous errors. I don't understand because the script starts by dropping all constraints. Could someone

Cost of creating dbcontext per web request on ASP.Net

主宰稳场 提交于 2020-04-11 02:57:31
问题 I am using Unit of work and Repository pattern along with EF6 in my asp.net web application. DbContext object is getting created and destroyed on every request. I am thinking that it is costly creating the new dbcontext on every request(I have not done any performance bench marking). Is this cost of creating DbContext on every request can be ignored ? Does anybody done some bench marking? 回答1: Creating a new context is ridiculously cheap, on the order of about 137 ticks on average (0.0000137

Cost of creating dbcontext per web request on ASP.Net

a 夏天 提交于 2020-04-11 02:57:13
问题 I am using Unit of work and Repository pattern along with EF6 in my asp.net web application. DbContext object is getting created and destroyed on every request. I am thinking that it is costly creating the new dbcontext on every request(I have not done any performance bench marking). Is this cost of creating DbContext on every request can be ignored ? Does anybody done some bench marking? 回答1: Creating a new context is ridiculously cheap, on the order of about 137 ticks on average (0.0000137

BeginTransaction with IsolationLevel in EF Core

半城伤御伤魂 提交于 2020-04-09 18:41:12
问题 I'm trying to rewrite old library to use EntityFramework Core and I can't figure out how to begin transaction with specific isolation level. Previously I was able to do something like this: DbContext.Database.BeginTransaction(IsolationLevel.Snapshot); What is alternative implementation in the EntityFramework Core? 回答1: The EF Core code is exactly the same. DbContext.Database.BeginTransaction(IsolationLevel.Snapshot); The only difference is that in EF Core the method with isolation level (as

BeginTransaction with IsolationLevel in EF Core

我怕爱的太早我们不能终老 提交于 2020-04-09 18:41:11
问题 I'm trying to rewrite old library to use EntityFramework Core and I can't figure out how to begin transaction with specific isolation level. Previously I was able to do something like this: DbContext.Database.BeginTransaction(IsolationLevel.Snapshot); What is alternative implementation in the EntityFramework Core? 回答1: The EF Core code is exactly the same. DbContext.Database.BeginTransaction(IsolationLevel.Snapshot); The only difference is that in EF Core the method with isolation level (as

How to check if my SQL Server Express database exceeds the 10 GB size limit?

心已入冬 提交于 2020-04-08 18:22:05
问题 I am developing a web site, it uses SQL Server 2008 R2 Express for its database. And in testing, there is a lot of data and images stored into this database. According to wiki, the SQL Server Express edition has a 10 GB size limit. When I insert data and reach the limit, what exception will be thrown? Or, how do I detect the approaching limit problem by codes ? I use EF 5 with code-first approach to insert large data set. 回答1: In tests I have seen that: sp_spaceused won't work as expected, it

Entity Framework circular dependency for last entity

坚强是说给别人听的谎言 提交于 2020-04-08 09:22:05
问题 Please consider the following entities public class What { public int Id { get; set; } public string Name { get; set; } public ICollection<Track> Tracks { get; set; } public int? LastTrackId { get; set; }] public Track LastTrack { get; set; } } public class Track { public Track(string what, DateTime dt, TrackThatGeoposition pos) { What = new What { Name = what, LastTrack = this }; } public int Id { get; set; } public int WhatId { get; set; } public What What { get; set; } } I use the

Create table and insert data into it during EF code first migration

倖福魔咒の 提交于 2020-04-08 02:44:12
问题 I'm using Entity Framework Code First with Code First migrations. During a migration, I need to create a new table, and then insert some data into it. So I create the table with : CreateTable("MySchema.MyNewTable", c => new { MYCOLUMNID = c.Int(nullable: false, identity: true), MYCOLUMNNAME = c.String(), }) .PrimaryKey(t => t.MYCOLUMNID); Then I try to insert data with : using (var context = new MyContext()) { context.MyNewTableDbSet.AddOrUpdate(new[] { new MyNewTable { MYCOLUMNNAME = "Test"