Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecify decimal scale as an attribute using EF code first
    text
    copied!<p>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.</p> <p>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.</p> <p>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 :</p> <pre><code>public class Transaction { [Key] public int ID { get; set; } ... [DisplayName("Exchange Rate")] [DecimalPrecision(18, 6)] public decimal? ExchangeRate { get; set; } } </code></pre> <p>What I do <strong>not</strong> want to do is update the <code>OnModelCreating</code> function as suggesting <a href="https://stackoverflow.com/questions/3504660/entity-framework-code-first-decimal-precision-and-scale">in this question</a></p> <p>Over <a href="http://geekswithblogs.net/danemorgridge/archive/2010/12/20/ef4-code-first-control-unicode-and-decimal-precision-scale-with.aspx" rel="nofollow noreferrer">here</a> 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.</p> <p>An alternative is proposed <a href="http://dotnetspeak.com/2011/03/custom-conventions-in-entity-framework-code-first-v-4-1" rel="nofollow noreferrer">here</a>, 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload