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

Entity Framework, Code First and Full Text Search

Using interceptors introduced in EF6, you could mark the full text search in linq and then replace it in dbcommand as described in http://www.entityframework.info/Home/FullTextSearch: public class FtsInterceptor : IDbCommandInterceptor { private const string FullTextPrefix = “-FTSPREFIX-“; public static string Fts(string search) { return string.Format(“({0}{1})”, FullTextPrefix, search); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { } … 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

Mapping Database Views to EF 5.0 Code First w/Migrations

You have specified that the ClientStatisticsView entity should be mapped to a table named “ClientStatistics”. So entity framework will generate a migration containing an instruction to create that table. But you have independently created that view in the database so to prevent the error you are getting you should remove the CreateTable instruction from the … Read more

Defining multiple Foreign Key for the Same table in Entity Framework Code First

To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called … Read more

Entity Framework Code First Lazy Loading

This is wrong “virtual” keyword is used for not loading the entities unless you explicit this (using an “Include” statement) Lazy Loading means that entities will be automatically loaded when you first access collection or navigation property, and that will happen transparently, as though they were always loaded with parent object. Using “include” is loading … Read more