When should I call SaveChanges() when creating 1000’s of Entity Framework objects? (like during an import)

I would test it first to be sure. Performance doesn’t have to be that bad. If you need to enter all rows in one transaction, call it after all of AddToClassName class. If rows can be entered independently, save changes after every row. Database consistence is important. Second option I don’t like. It would be … Read more

Equivalent for .HasOptional in Entity Framework Core 1 (EF7)

You will not find an equivalent method in EF 7. By convention, a property whose CLR type can contain null will be configured as optional. So what decide if the relationship is optional or not is if the FK property is nullable or not respectively. In summary, due to your Message_Id FK property is string, … Read more

Set decimal(16, 3) for a column in Code First Approach in EF4.3 [duplicate]

The DataType Attribute is a Validation Attribute. You need to do that using the ModelBuilder. public class MyContext : DbContext { public DbSet<MyClass> MyClass; protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<MyClass>().Property(x => x.SnachCount).HasPrecision(16, 3); modelBuilder.Entity<MyClass>().Property(x => x.MinimumStock).HasPrecision(16, 3); modelBuilder.Entity<MyClass>().Property(x => x.MaximumStock).HasPrecision(16, 3); } }

How do I singularize my tables in EF Code First?

You’ve removed the wrong convention (PluralizingEntitySetNameConvention) for this purpose. Just replace your OnModelCreating method with the below and you will be good to go. using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db; … protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } With Entity Framework 6, on your file that inherit from DbContext: using System.Data.Entity.ModelConfiguration.Conventions; protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); … Read more

Multi-async in Entity Framework 6?

The exception explains clearly that there is only one asynchronous operation per context allowed at a time. So, you either have to await them one at a time as the error message suggests: var banner = await context.Banners.ToListAsync(); var newsGroup = await context.NewsGroups.ToListAsync(); Or you can use multiple contexts: var banner = context1.Banners.ToListAsync(); var newsGroup … Read more

Entity Framework CTP 4 – Code First Custom Database Initializer

I ran into the same problem. I didn’t really solve it, but I managed to get a little nasty workaround running, so i can deploy my solution to AppHarbor 😉 Its a IDatabaseInitializer implementation, that doesn’t delete the db, but just nukes all the constraints and tables, and then uses the ObjectContext.CreateDatabaseScript() method to generate … Read more