Entity Framework Code First Fluent Api: Adding Indexes to columns

After Migrations was introduced in EF 4.3 you can now add indexes when modifying or creating a table. Here is an excerpt from the EF 4.3 Code-Based Migrations Walkthrough from the ADO.NET team blog namespace MigrationsCodeDemo.Migrations { using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up() { CreateTable( “Posts”, c => … 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

Using Entity Framework (code first) migrations in production

There is a Database Initializer you can use to achieve the migration to latest version on startup (or better, the dbinitializer will kick in on first db access), the MigrateDatabaseToLatestVersion, you use it like that: Database.SetInitializer<ObjectContext>( new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>()); Regarding having one file per migration, if you enable automatic migrations you will find them in … Read more

what is the most reasonable way to find out if entity is attached to dbContext or not?

If you are using DbContext API (you mentioned ef-code-first) you can simply use: context.YourEntities.Local.Any(e => e.Id == id); or more complex context.ChangeTracker.Entries<YourEntity>().Any(e => e.Entity.Id == id); In case of ObjectContext API you can use: context.ObjectStateManager.GetObjectStateEntries(~EntityState.Detached) .Where(e => !e.IsRelationship) .Select(e => e.Entity) .OfType<YourEntity>() .Any(x => x.Id == id);

How to create a table corresponding to enum in EF6 Code First?

Since EF doesn’t handle it automatically, yes, this is the recommend way. I suggest some modifications in article that you provided. Rename your enum public enum FacultyEnum { Eng, Math, Eco } Create a class that represent the table public class Faculty { private Faculty(FacultyEnum @enum) { Id = (int)@enum; Name = @enum.ToString(); Description = … 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

EF codefirst : Should I initialize navigation properties?

Collections: It doesn’t matter. There is a distinct difference between collections and references as navigation properties. A reference is an entity. A collections contains entities. This means that initializing a collection is meaningless in terms of business logic: it does not define an association between entities. Setting a reference does. So it’s purely a matter … Read more

One to one optional relationship using Entity Framework Fluent API

EF Code First supports 1:1 and 1:0..1 relationships. The latter is what you are looking for (“one to zero-or-one”). Your attempts at fluent are saying required on both ends in one case and optional on both ends in the other. What you need is optional on one end and required on the other. Here’s an … Read more