Entity Framework loading child collection with sort order

You cannot achieve it directly because neither eager or lazy loading in EF supports ordering or filtering. Your options are: Sort data in your application after you load them from database Execute separate query to load child records. Once you use separate query you can use OrderBy The second option can be used with explicit … Read more

Asp.Net Web API Error: The ‘ObjectContent`1’ type failed to serialize the response body for content type ‘application/xml; charset=utf-8’

I would suggest Disable Proxy Creation only in the place where you don’t need or is causing you trouble. You don’t have to disable it globally you can just disable the current DB context via code… [HttpGet] [WithDbContextApi] public HttpResponseMessage Get(int take = 10, int skip = 0) { CurrentDbContext.Configuration.ProxyCreationEnabled = false; var lista = … Read more

Add a DbProviderFactory without an App.Config

The following will probably cause sunspots and overthrow western civilization. It may even cause a debate about Duct Tape Programming (make it stop!), but it works (for now) try { var dataSet = ConfigurationManager.GetSection(“system.data”) as System.Data.DataSet; dataSet.Tables[0].Rows.Add(“SQLite Data Provider” , “.Net Framework Data Provider for SQLite” , “System.Data.SQLite” , “System.Data.SQLite.SQLiteFactory, System.Data.SQLite”); } catch (System.Data.ConstraintException) { … Read more

Best way to incrementally seed data in Entity Framework 4.3

If you want to use entities to seed data you should use Seed method in your migrations configuration. If you generate fresh project Enable-Migrations you will get this configuration class: internal sealed class Configuration : DbMigrationsConfiguration<YourContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(CFMigrationsWithNoMagic.BlogContext context) { // This method will be … Read more

How to get primary key value with Entity Framework Core

I also faced with similar problem and found the following solution // Entity Framework Core public virtual int GetKey<T>(T entity) { var keyName = Context.Model.FindEntityType(typeof (T)).FindPrimaryKey().Properties .Select(x => x.Name).Single(); return (int)entity.GetType().GetProperty(keyName).GetValue(entity, null); }

EF can’t infer return schema from Stored Procedure selecting from a #temp table

CREATE PROCEDURE [MySPROC] AS BEGIN –supplying a data contract IF 1 = 2 BEGIN SELECT cast(null as bigint) as MyPrimaryKey, cast(null as int) as OtherColumn WHERE 1 = 2 END CREATE TABLE #tempSubset( [MyPrimaryKey] [bigint] NOT NULL, [OtherColumn] [int] NOT NULL) INSERT INTO #tempSubset (MyPrimaryKey, OtherColumn) SELECT SomePrimaryKey, SomeColumn FROM SomeHugeTable WHERE LimitingCondition = true … Read more