Generate money type fields using code first EF CTP5
Asked Answered
E

2

29

In this blog post: EF4 Code First Control Unicode and Decimal Precision, Scale with Attributes, Dane Morgridge used attributes to control the creation of different types on your database.

...And I found this pretty unique BTW!!!

How do I generate money type fields in my resulting database using code first API of EF CTP5, if is possible to do it from your model, using conventions or attributes?

Sorry about my English is not my main language.

Thanks in advance.

Encephalon answered 28/1, 2011 at 15:16 Comment(3)
In general (and unless you're working with an existing database schema), I'd avoid using the SQL money datatype. You're better off using decimals with specific precision and scale that meet your application requirementsKindliness
@Damien interesting... why is that?Ss
Related, for working with EF migrations with money-type params: #27697228Liverwort
B
54

For example, consider this Invoice class:

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

You can do it with fluent API:

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

Or you can do it with Data Annotations:

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

    [Column(TypeName="Money")]
    public decimal Amount { get; set; }
}
Bilyeu answered 28/1, 2011 at 22:1 Comment(2)
Unfortunately, EF 4.3 (release version) ignores money data type and creates decimal(18,0) definition instead (Does not honor Column attribute and HasColumnType configurations). This definitely is a bug.Emission
EF 4.3.1 fixed this issue where the Column attribute was not being honoured. Using [Column(TypeName="Money")] works as expected now. blogs.msdn.com/b/adonet/archive/2012/02/29/…Jasun
B
12
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 like that

[Money]
public decimal Value { get; set; }
Brutality answered 28/1, 2011 at 23:19 Comment(5)
BinaryPropertyConfiguration doesn't have the HasColumnType member, I try this approach but it didn't workAlready
Sorry, the correct is configuration.ColumnType = "Money";. I just edit the post. Thanks.Brutality
ahahaha, omg i'm so sorry my fault.. post edited and now it should work.. thanks buddyBrutality
I thought EF Conventions were removed from Code First prior to release?Bascule
Convention was removed at version 4.1. Probably will be released in the future.Brutality

© 2022 - 2024 — McMap. All rights reserved.