ADO.Net EF – how to define foreign key relation in model first approach?

First of all your model is probably wrong. ConsoleType and Game is not in one-to-one relation (unless you have single game for each console type). I expect 1 console can have multiple games. So it should be one-to-many. In reality game can be released on multiple platforms so it should be many-to-many. You got unvanted … Read more

EF4 – The selected stored procedure returns no columns

EF doesn’t support importing stored procedures which build result set from: Dynamic queries Temporary tables The reason is that to import the procedure EF must execute it. Such operation can be dangerous because it can trigger some changes in the database. Because of that EF uses special SQL command before it executes the stored procedure: … Read more

Entity framework self referencing loop detected [duplicate]

Well the correct answer for the default Json formater based on Json.net is to set ReferenceLoopHandling to Ignore. Just add this to the Application_Start in Global.asax: HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter .SerializerSettings .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; This is the correct way. It will ignore the reference pointing back to the object. Other responses focused in changing … Read more

What are Independent Associations and Foreign Key Associations? [duplicate]

Just my opinions about WHY to use independent or foreign key associations: Independent associations Pros: This is correct way to go in object oriented world. In object oriented world we are using references inside our aggregates, not some magic keys. Cons: With pure POCO you don’t know if principal relation is really NULL or just … Read more

Disable lazy loading by default in Entity Framework 4

The following answer refers to Database-First or Model-First workflow (the only two workflows that were available with Entity Framework (version <= 4.0) when the question was asked). If you are using Code-First workflow (which is available since EF version >= 4.1) proceed to ssmith’s answer to this question for a correct solution. The edmx file … Read more

EF Including Other Entities (Generic Repository pattern)

Use just the Include extension on IQueryable. It is available in EF 4.1 assembly. If you don’t want to reference that assembly in your upper layers create wrapper extension method in your data access assembly. Here you have example: public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params Expression<Func<T, object>>[] includes) where T : class { if … Read more

Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable

Start with you DbContext, create a new file called Database.cs: Database.cs public class Database : DbContext { private IDbSet<Post> _posts; public IDbSet<Post> Posts { get { return _posts ?? (_posts = DbSet<Post>()); } } public virtual IDbSet<T> DbSet<T>() where T : class { return Set<T>(); } public virtual void Commit() { base.SaveChanges(); } } Define … Read more

The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

You cannot use properties that are not mapped to a database column in a Where expression. You must build the expression based on mapped properties, like: var date = DateTime.Now.AddYears(-from); result = result.Where(p => date >= p.DOB); // you don’t need `AsQueryable()` here because result is an `IQueryable` anyway As a replacement for your not … Read more

Entity Framework Timeouts

There is a known bug with specifying default command timeout within the EF connection string. http://bugs.mysql.com/bug.php?id=56806 Remove the value from the connection string and set it on the data context object itself. This will work if you remove the conflicting value from the connection string. Entity Framework Core 1.0: this.context.Database.SetCommandTimeout(180); Entity Framework 6: this.context.Database.CommandTimeout = … Read more