How can I prevent EF “The context cannot be used while the model is being created” errors?

I finally figured out the true cause of this, at least for me. The issue was that I was retrieving a DbContext from Windsor in my custom Asp.Net Membership provider. This caused an issue because the membership provider has a lifespan of the whole application, while all other retrieval calls for the db context were … Read more

Navigation Property without Declaring Foreign Key

I believe, it is not possible to define the relationship only with data attributes. The problem is that EF’s mapping conventions assume that Creator and Modifier are the two ends of one and the same relationship but cannot determine what the principal and what the dependent of this association is. As far as I can … Read more

Self referencing / parent-child relationship in Entity Framework

You must define the ParentId in the category class as nullable to use it as the foreign key property for an optional relationship: public int? ParentId { get; set; } An int property cannot take the value null and therefore cannot represent a NULL as value in a database column.

Primary /Foreign Key in Entity Framework

I think by “not using EF ORM designer” you mean new DbContext API from EF 4.1. Because if you don’t mean DbContext API you still have to use EDMX (designer). You can either use data annotations (System.ComponentModel.DataAnnotations): KeyAttribute and ForeignKeyAttribute: public class Registry { public virtual int Id { get; set; } public virtual MyEntity … Read more

Command Timeout with Entity Framework 4.1 Code First

I found this solution after another Google search. You can access the ObjectContext for a DbContext by casting this to an IObjectContextAdapter. From http://social.msdn.microsoft.com/Forums/en-ZA/adodotnetentityframework/thread/6fe91a64-0208-4ab8-8667-d061af340994: public class MyContext : DbContext { public MyContext () : base(ContextHelper.CreateConnection(“my connection string”), true) { ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 300; } }

Entity Framework 4.1 InverseProperty Attribute and ForeignKey

It is theoretically correct but SQL server (not Entity framework) doesn’t like it because your model allows single employee to be a member of both First and Second team. If the Team is deleted this will cause multiple delete paths to the same Employee entity. This cannot be used together with cascade deletes which are … Read more