Creating Money Type Fields Using EF CTP5 Code

In this blog post: EF4 Code First Control Unicode and decimal precision, scale with attributes , Dane Morgridge attributes to control the creation of different types in your database.

... and I found this rather unique BTW !!!

How to create money type fields in my resulting database using the first EF CTP5 API, if it is possible to do this from your model using conventions or attributes?

Sorry that my English is not my main language.

Thanks in advance.

+20
entity-framework ef-code-first entity-framework-ctp5
Jan 28 2018-11-18T00:
source share
2 answers

For example, consider the Invoice class:

public class Invoice { public int InvoiceId { get; set; } public decimal Amount { get; set; } } 

You can do this using the free API:

 protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Invoice>() .Property(i => i.Amount) .HasColumnType("Money"); } 

Or you can do this with data annotations:

 public class Invoice { public int InvoiceId { get; set; } [Column(TypeName="Money")] public decimal Amount { get; set; } } 
+38
Jan 28 '11 at 10:01
source share
 using System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive; public class MoneyAttribute : Attribute { } public class MoneyAttributeConvention : AttributeConfigurationConvention<PropertyInfo, DecimalPropertyConfiguration, MoneyAttribute> { public override void Apply(PropertyInfo memberInfo, DecimalPropertyConfiguration configuration, MoneyAttribute attribute) { configuration.ColumnType = "money"; } } 

then you use so

 [Money] public decimal Value { get; set; } 
+11
Jan 28 '11 at 23:19
source share



All Articles