Generate money type fields using code first EF CTP5

后端 未结 2 1374
野性不改
野性不改 2020-12-06 04:10

In this blog post: EF4 Code First Control Unicode and Decimal Precision, Scale with Attributes, Dane Morgridge used attributes to control the creation of di

2条回答
  •  执念已碎
    2020-12-06 04:53

    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()
                    .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; }
    }
    

提交回复
热议问题