dbcontext

EF 4.1 - DBContext SqlQuery and Include

时间秒杀一切 提交于 2019-12-04 03:20:00
问题 I want to execute a raw sql using DBContext SqlQuery and then include related entites. I've tried the following but it doesn't load the related entities: string sql = "Select * from client where id in (select id from activeclient)"; var list = DbContext.Database.SqlQuery<Client>(sql).AsQueryable().Include(c => c.Address).Include(c => c.Contactinfo).ToList(); Any help? 回答1: It is not possible. Include works only with ESQL or linq-to-entities because it must be processed during query building

Top per group: Take(1) works but FirstOrDefault() doesn't?

心已入冬 提交于 2019-12-04 02:51:12
I'm using EF 4.3.1 ... just upgraded to 4.4 (problem remains) with database-first POCO entities generated by the EF 4.x DbContext Generator . I have the following database named 'Wiki' (SQL script to create tables and data is here ): When a wiki article is edited, instead of its record being updated, the new revision is inserted as a new record with the revision counter incremented. In my database there is one author, "John Doe", which has two articles, "Article A" and "Article B", where article A has two version (1 and 2), but article B has only one version. I have both lazy loading and proxy

SqlException: Syntax Error Near 'GO'

牧云@^-^@ 提交于 2019-12-04 02:49:00
问题 I am having trouble sending a SQL statement through a DbContext using context.Database.ExecuteSqlCommand() . I am trying to execute CREATE TABLE Phones([Id] [uniqueidentifier] NOT NULL PRIMARY KEY, [Number] [int],[PhoneTypeId] [int]) GO ALTER TABLE [dbo].[Phones] ADD CONSTRAINT [DF_Phones_Id] DEFAULT (newid()) FOR [Id] GO This fails with the error string Incorrect syntax near the keyword 'ALTER'. Incorrect syntax near 'GO'. However running that exact statement in SSMS runs without errors? Any

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

我怕爱的太早我们不能终老 提交于 2019-12-04 02:22:46
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. System.ObjectDisposedException: Safe handle has been closed Microsoft.EntityFrameworkCore.Query

How Can I Use Custom Validation Attributes on Child Models of a DB Entity?

半城伤御伤魂 提交于 2019-12-04 00:28:52
问题 Summary: I want a data annotation validator to reference another property in the same class ( TitleAuthorAndPublishingConfiguration ). However, DB.SaveChanges() is not being called on this class directly. Rather it is being called on the parent of this class ( WebsiteConfiguration ). Therefore validationContext.ObjectType is returning WebsiteConfiguration and I am unable to refer to properties of TitleAuthorAndPublishingConfiguration within the data annotation validator. WebsiteConfiguration

Is DbSet<>.Local something to use with special care?

你。 提交于 2019-12-03 16:53:47
问题 For a few days now, I have been struggling with retrieving my entities from a repository ( DbContext ). I am trying to save all the entities in an atomic action. Thus, different entities together represent something of value to me. If all the entities are 'valid', then I can save them all to the database. Entity 'a' is already stored in my repository, and needs to be retrieved to 'validate' entity 'b'. That's where the problem arises. My repository relies on the DbSet<TEntity> class which

Entity Framework, Code First, Update “one to many” relationship with independent associations

主宰稳场 提交于 2019-12-03 16:23:17
问题 It took me way too long to find a solution to the scenario described below. What should seemingly be a simple affair proved to be rather difficult. The question is: Using Entity Framework 4.1 (Code First approach) and "Independent associations" how do I assign a different end to an existing "many to one" relationship in a "detached" scenario ( Asp.Net in my case). The model: I realize that using ForeignKey relationships instead of Independent Associations would have been an option, but it was

Should I separate my application context from ApplicationDbContext used for identity?

瘦欲@ 提交于 2019-12-03 15:43:58
问题 In Visual-Studio 2013, when creating an ASP.NET project, it generates a file IdentityModels.cs that contains a class ApplicationDbContext , that inherits from IdentityDbContext<ApplicationUser> , which eventually inherits from DbContext . Should I keep this context only for account-related entities, and created a separate context for all the other entities in the application or should I mix it. Any security issue or reason not to include all the entities of my entire application in one

Manually Provide DbContext to DbMigrator

我是研究僧i 提交于 2019-12-03 15:42:05
Platform .NET 4.5 and Entity Framework 6. Question I have the following code to execute a Migration: //The following function just returns an object of the Configuration() class //generated by code migrations var migratorConfig = currentMigrationProvider.CreateDbMigrationConfiguration(); var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(migratorConfig); dbMigrator.Update(); The problem is that Update() function tries to create an instance of my DbContext class and for a few good reasons I need to manually create the context and feed it to dbMigrator. Is that possible? How? Yes it

EF CodeFirst: Get all POCO types for DbContext

旧城冷巷雨未停 提交于 2019-12-03 13:16:32
Is there any way to get POCO's types from specified DbContext instance? You need to access the MetadataWorkspace public class MyContext : DbContext { public void Test() { var objectContext = ((IObjectContextAdapter)this).ObjectContext; var mdw = objectContext.MetadataWorkspace; var items = mdw.GetItems<EntityType>(DataSpace.CSpace); foreach (var i in items) { Console.WriteLine("Class Name: {0}", i.Name); } } @Lei Yang: you can use this to get all types into a list. var objectContext = ((IObjectContextAdapter) dbContext).ObjectContext; var mdw = objectContext.MetadataWorkspace; var items = mdw