Under EF4.3, in the Code First Approach, set decimal(16, 3) for a column.

ef-code-first entity-framework sql-types

Question

How can I do this :

private decimal _SnachCount;
[Required]
[DataType("decimal(16 ,3")]
public decimal SnachCount
{
    get { return _SnachCount; }
    set { _SnachCount = value; }
}

private decimal _MinimumStock;
[Required]
[DataType("decimal(16 ,3")]
public decimal MinimumStock
{
    get { return _MinimumStock; }
    set { _MinimumStock = value; }
}

private decimal _MaximumStock;
[Required]
[DataType("decimal(16 ,3")]
public decimal MaximumStock
{
    get { return _MaximumStock; }
    set { _MaximumStock = value; }
}

After generating the database by this part of my model , these three columns type are decimal(18,2),why? what is this code error? how can i do that ?

1
44
1/27/2012 11:55:03 AM

Accepted Answer

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);
    }
}
76
1/27/2012 12:28:45 PM

Popular Answer

You can modify all decimal propreties in database. In your DBContext in method OnModelCreating add line:

modelBuilder.Properties<decimal>().Configure(c => c.HasPrecision(18, 3));


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow