Specify decimal scale as an attribute using EF code first

c# ef-code-first entity-framework

Question

I'm using Entity Framework Code First to query my SQL Server database. I'm also using code migrations to keep it up to date.

When I use the decimal type, the underlying columns are created with a precision/scale of 18,2. For one particular column (exchange rate) I would like to increase this to 18,6.

I'd like to add an attribute to my class that will instruct EF to use the specified decimal precision, as in the following example :

public class Transaction
{
    [Key]
    public int ID { get; set; }

    ...

    [DisplayName("Exchange Rate")]
    [DecimalPrecision(18, 6)]
    public decimal? ExchangeRate { get; set; }

}

What I do not want to do is update the OnModelCreating function as suggesting in this question

Over here I found a potential solution, but I then discovered that code won't compile as it is based on pre-release functionality that was then removed.

An alternative is proposed here, but that looks way complicated, and perhaps before I wade in I thought I might ask if an alternative exists? EF is moving so fast it's hard to keep up with the evolutions, perhaps I missed something.

1
3
5/23/2017 11:47:35 AM

Popular Answer

I created that same custom attribute and the code to run it you can see the answer here https://stackoverflow.com/a/15386883/1186032

Good luck!

2
5/23/2017 10:30:01 AM


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