Entity framework core 实体框架7为模型生成器设置十进制精度

Entity framework core 实体框架7为模型生成器设置十进制精度,entity-framework-core,Entity Framework Core,我一直在试图弄清楚如何设置EF7(Beta 4)的十进制精度,但运气不好 我期待着做一些类似的事情: modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6) 用法示例: modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPr

我一直在试图弄清楚如何设置EF7(Beta 4)的十进制精度,但运气不好

我期待着做一些类似的事情:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)
用法示例:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6);

由于我还没有尝试过这一点,我不确定“HasColumnType”或“ForSqlServerHasColumnType”之间的用法是否正确,但希望这将为某人指明正确的方向。

您的解决方案就是我们想要的设计。你可以设置诸如精度、比例、最大长度、unicode/ansi、固定/可变长度等类型,而不是一堆“facet”。我们决定保持简单:如果默认类型映射不是你想要的,请告诉我们使用什么类型。有人谈论要推翻这一决定,重新引入“方面”。如果你对此有强烈的感觉,我鼓励你这样做


还请注意,目前类型映射中还有许多其他错误,但在我们发布beta5时,这些错误应该已经修复。

根据EF RC1,所示示例似乎已经过时

下面是我如何设置十进制字段的精度

假设我有一个实体

public class Review
{
    public int ReviewId { get; set; }
    public decimal TotalScore { get; set; } //I want a precision field in DB
    public DateTime CreatedOn { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
}
然后在我的上下文类中,在创建模型时,我实例化了映射(我可以在那里进行映射,但我喜欢将其分开)

公共类MyDbContext:DbContext
{
公共MyDbContext(DbContextOptions):基本(选项)
{
}
公共数据库集评论{get;set;}
//等等。
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
//映射
新的ReviewMap(modelBuilder.Entity());
//等等。。
}
}
然后是映射。请记住使用模型扩展所在的命名空间:

using Microsoft.Data.Entity; //here is where the extensions are
public class ReviewMap
{
    public ReviewMap(EntityTypeBuilder<Review> entityBuilder)
    {
        entityBuilder.HasKey(r => r.ReviewId);

        //Using the column type extension
        entityBuilder.Property(r => r.TotalScore)
            .HasColumnType($"decimal(5,2)")
            .IsRequired(true);

        //and this has nothing to do with the example but it's interesting
        //to show how to use Sql command to automatically fulfil a value 
        //when adding a new Entity
        entityBuilder.Property(r => r.CreatedOn)
            .ValueGeneratedOnAdd()
            .HasDefaultValueSql("GETUTCDATE()")
            .IsRequired(true);
    }
}
使用Microsoft.Data.Entity//这里是扩展的地方
公开课复习地图
{
公共审核地图(EntityTypeBuilder entityBuilder)
{
entityBuilder.HasKey(r=>r.ReviewId);
//使用列类型扩展名
entityBuilder.Property(r=>r.TotalScore)
.HasColumnType($“十进制(5,2)”)
.IsRequired(正确);
//这与示例无关,但很有趣
//演示如何使用Sql命令自动实现值
//添加新实体时
属性(r=>r.CreatedOn)
.ValueGeneratedOnAdd()
.HasDefaultValueSql(“GETUTCDATE()”)
.IsRequired(正确);
}
}

感谢您的澄清,我用一个简单的扩展名编辑了这个问题,以避免使用字符串。我不会对所有这些被重新引入的情况感到惊讶,但我不认为这会造成问题,因为你们有更大的问题要做:)我感谢你们所做的所有工作,我真的很期待更多地使用EF7!由于我所有的十进制属性都具有相同的精度,有没有办法在一行中实现这一点?我在EF6中使用了它:
modelBuilder.Properties().Configure(x=>x.HasPrecision(18,6))@reala valoro,我已经更新了详细信息以反映RC1的变化
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6);
    public static PropertyBuilder DecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale)
    {
        return propertyBuilder.HasColumnType($"decimal({precision},{scale})");
    }

    public static PropertyBuilder SqlDecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale)
    {
        return propertyBuilder.ForSqlServerHasColumnType($"decimal({precision},{scale})");
    }
public class Review
{
    public int ReviewId { get; set; }
    public decimal TotalScore { get; set; } //I want a precision field in DB
    public DateTime CreatedOn { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
}
public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options ) : base(options)
    {
    }

    public DbSet<Review> Reviews { get; set; }
    //etc.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //Mappings
        new ReviewMap(modelBuilder.Entity<Review>());
        //etc..
    }
}
using Microsoft.Data.Entity; //here is where the extensions are
public class ReviewMap
{
    public ReviewMap(EntityTypeBuilder<Review> entityBuilder)
    {
        entityBuilder.HasKey(r => r.ReviewId);

        //Using the column type extension
        entityBuilder.Property(r => r.TotalScore)
            .HasColumnType($"decimal(5,2)")
            .IsRequired(true);

        //and this has nothing to do with the example but it's interesting
        //to show how to use Sql command to automatically fulfil a value 
        //when adding a new Entity
        entityBuilder.Property(r => r.CreatedOn)
            .ValueGeneratedOnAdd()
            .HasDefaultValueSql("GETUTCDATE()")
            .IsRequired(true);
    }
}