Generate money type fields using code first EF CTP5

Deadly 提交于 2019-11-27 22:43:35

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; }
}
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; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!