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

“like” queries in Entity Framework

This guy made a very nice “WhereLike” extension for Linq that accepts any wildcard character and compares two values (one of which comes from an expression) with a generic method derived from the location of the wildcard. x% -> startswith %x -> endswith %x% -> contains http://trentacular.com/2010/08/linq-to-entities-wild-card-like-extension-method/ EDIT: The article seems to be down. I … Read more

Entity Framework 4.2 exec sp_executesql does not use indexes (parameter sniffing)

tl;dr update statistics We had a delete query with one parameter (the primary key) that took ~7 seconds to complete when called through EF and sp_executesql. Running the query manually, with the parameter embedded in the first argument to sp_executesql made the query run quickly (~0.2 seconds). Adding option (recompile) also worked. Of course, those … Read more

Unique keys in Entity Framework 4

The Entity Framework 6.1 now supports uniques with both Data Annotations and Fluent API. Data Annotations (Reference) public class MyEntityClass { [Index(IsUnique = true)] [MaxLength(255)] // for code-first implementations public string MyUniqueProperty{ get; set; } } Fluent API (Reference) public class MyContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder .Entity<MyEntityClass>() .Property(t => … Read more