What is the difference between DbSet and virtual DbSet?

public class AppContext : DbContext { public AppContext() { Configuration.LazyLoadingEnabled = true; } public virtual DbSet<AccountType> AccountTypes { get; set; } } public class AccountType { public Guid Id { get; set; } public string Name { get; set; } public virtual ICollection<AccountCode> AccountCodes { get; set; } } public class AccountCode { public Guid … Read more

Many-to-many mapping table

Do this on your DbContext OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Recipe>() .HasMany(x => x.Members) .WithMany(x => x.Recipes) .Map(x => { x.ToTable(“Cookbooks”); // third table is named Cookbooks x.MapLeftKey(“RecipeId”); x.MapRightKey(“MemberId”); }); } You can do it the other way around too, it’s the same, just another side of the same coin: modelBuilder.Entity<Member>() .HasMany(x => … Read more

How to sync model after using Code First from Database using Entity Framework 6.1 and MVC 5?

The Entity Data Model Wizard’s Code First from Database does an excellent job creating your entity classes, as if they were created in the Code First style. What you are asking is if there is any way to keep these classes up-to-date as your database changes, similar to the EDMX style “Update Model From Database”. … Read more

Entity framework, problems updating related objects

CurrentValues.SetValues only updates scalar properties but no related entities, so you must do the same for each related entity: public Foo Edit(Foo newFoo) { var dbFoo = context.Foo .Include(x => x.SubFoo) .Include(x => x.AnotherSubFoo) .Single(c => c.Id == newFoo.Id); context.Entry(dbFoo).CurrentValues.SetValues(newFoo); context.Entry(dbFoo.SubFoo).CurrentValues.SetValues(newFoo.SubFoo); context.Entry(dbFoo.AnotherSubFoo).CurrentValues.SetValues(newFoo.AnotherSubFoo); context.SaveChanges(); return newFoo; } If the relationship could have been removed altogether or … Read more

Unique key with EF code first

Unfortunately you can’t define it as unique key in code first because EF doesn’t support unique keys at all (it is hopefully planned for next major release). What you can do is to create custom database intializer and add unique index manually by calling SQL command: public class MyInitializer : CreateDatabaseIfNotExists<MyContext> { protected override void … Read more

How to Seed Users and Roles with Code First Migration using Identity ASP.NET Core

My way of doing this is to create a class in models namespace. public class SampleData { public static void Initialize(IServiceProvider serviceProvider) { var context = serviceProvider.GetService<ApplicationDbContext>(); string[] roles = new string[] { “Owner”, “Administrator”, “Manager”, “Editor”, “Buyer”, “Business”, “Seller”, “Subscriber” }; foreach (string role in roles) { var roleStore = new RoleStore<IdentityRole>(context); if (!context.Roles.Any(r … Read more

Mocking or faking DbEntityEntry or creating a new DbEntityEntry

Just like the other case, what you need is to add an additional level of indirection: interface ISalesContext { IDbSet<T> GetIDbSet<T>(); void SetModified(object entity) } class SalesContext : DbContext, ISalesContext { public IDbSet<T> GetIDbSet<T>() { return Set<T>(); } public void SetModified(object entity) { Entry(entity).State = EntityState.Modified; } } So, instead of calling the implementation, you … Read more

How do I eagerly Include the child and grandchild elements of an entity in Entity Framework Code First?

Also, it isn’t necessary to use the string overload. This method will work too: var customers = db.Customers.Include(c => c.Books.Select(b => b.Author)); For more examples see the EF team blog post: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx And this tutorial: http://www.asp.net/entity-framework/tutorials/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application