Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API

By changing pocos to: public class Order { public int OrderId { get; set; } public virtual Quotation Quotation { get; set; } } public class Quotation { public int QuotationId { get; set; } public virtual Order Order { get; set; } } and using these mapping files: public class OrderMap : EntityTypeConfiguration<Order> { … Read more

Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

Entity Framework 6 added support for multiple DbContexts by adding the -ContextTypeName and -MigrationsDirectory flags. I just ran the commands in my Package Manager Console and pasted the output below… If you have 2 DbContexts in your project and you run enable-migrations, you’ll get an error (as you probably already know): PM> enable-migrations More than … Read more

How can I force entity framework to insert identity columns?

EF 6 method, using the msdn article: using (var dataContext = new DataModelContainer()) using (var transaction = dataContext.Database.BeginTransaction()) { var user = new User() { ID = id, Name = “John” }; dataContext.Database.ExecuteSqlCommand(“SET IDENTITY_INSERT [dbo].[User] ON”); dataContext.User.Add(user); dataContext.SaveChanges(); dataContext.Database.ExecuteSqlCommand(“SET IDENTITY_INSERT [dbo].[User] OFF”); transaction.Commit(); } Update: To avoid error “Explicit value must be specified for identity … 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

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

How can I automatically filter out soft deleted entities with Entity Framework?

I’ve got soft delete working for all my entities and soft deleted items are not retrieved via the context using a technique suggested by this answer. That includes when you access the entity via navigation properties. Add an IsDeleted discriminator to every entity that can be soft deleted. Unfortunately I haven’t worked out how to … Read more

The entity type is not part of the model for the current context

Put this in your custom DbContext class: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Estate>().ToTable(“Estate”); } If your tables are not created on startup, this is why. You need to tell the DbContext about them in the OnModelCreating method override. You can either do custom per-entity mappings here, or separate them out into separate EntityTypeConfiguration<T> classes.

One DbContext per request in ASP.NET MVC (without IOC container)

I know this is not a recent question, but I’ll post my answer anyway, because I believe someone may find it useful. As probably many others, I followed the steps mentioned in the accepted answer. Yay, it works. HOWEVER, there’s one catch: Methods BeginRequest() and EndRequest() fire each time a request is made, but not … Read more