ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)

From a point of view of clean creation of POCO entities, there is no difference between the two generators. Both generators produce the same entities, however, ADO.NET POCO Entity Generator is based on ObjectContext‘s API, whereas ADO.NET DbContext Generator is based on DbContext‘s API. DbContext’s API has a few very nice new features (Local, Query … Read more

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

If you talk about dynamic proxies in EF there are two different types to distinguish: Proxies for lazy loading Proxies for change tracking Usually a change tracking proxy also can serve as a proxy for lazy loading. The reverse is not true. This is because the requirements for change tracking proxies are higher, especially all … Read more

Why is inserting entities in EF 4.1 so slow compared to ObjectContext?

As already indicated by Ladislav in the comment, you need to disable automatic change detection to improve performance: context.Configuration.AutoDetectChangesEnabled = false; This change detection is enabled by default in the DbContext API. The reason why DbContext behaves so different from the ObjectContext API is that many more functions of the DbContext API will call DetectChanges … Read more

Entity Framework code first. Find primary key

You can ask mapping metadata to get names of key properties (there can be more then one): ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectSet<YourEntity> set = objectContext.CreateObjectSet<YourEntity>(); IEnumerable<string> keyNames = set.EntitySet.ElementType .KeyMembers .Select(k => k.Name); Once you have key names you can use reflection to access their values. As you can see the approach reverts back to … Read more

How to retrieve Data Annotations from code? (programmatically)

Extension method: public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute { var attrType = typeof(T); var property = instance.GetType().GetProperty(propertyName); return (T)property .GetCustomAttributes(attrType, false).First(); } Code: var name = player.GetAttributeFrom<DisplayAttribute>(nameof(player.PlayerDescription)).Name; var maxLength = player.GetAttributeFrom<MaxLengthAttribute>(nameof(player.PlayerName)).Length;

Why is .Contains slow? Most efficient way to get multiple entities by primary key?

UPDATE: With the addition of InExpression in EF6, the performance of processing Enumerable.Contains improved dramatically. The analysis in this answer is great but largely obsolete since 2013. Using Contains in Entity Framework is actually very slow. It’s true that it translates into an IN clause in SQL and that the SQL query itself is executed … Read more

How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

If you have an Order class, adding a property that references another class in your model, for instance Customer should be enough to let EF know there’s a relationship in there: public class Order { public int ID { get; set; } // Some other properties // Foreign key to customer public virtual Customer Customer … Read more