Mvc Set Precision по десятичным знакам [дубликат]

$query = "SELECT Name,Mobile,Website,Rating FROM grand_table order by 4";

while( $data = mysql_fetch_array($query))
{
    echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td></tr>");      
}

Вместо использования запроса WHERE вы можете использовать этот запрос ORDER BY. Это намного лучше, чем это для использования запроса.

Я выполнил этот запрос и не получаю никаких ошибок, таких как параметр или логический.

184
задан kjbartel 25 January 2016 в 02:08
поделиться

14 ответов

Ответ от Дейва Ван ден Эйнда теперь устарел. Существует 2 важных изменения: начиная с EF 4.1, теперь класс ModelBuilder теперь DbModelBuilder , и теперь есть метод DecimalPropertyConfiguration.HasPrecision, который имеет подпись:

public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )

, где точность это общее количество разрядов, которые будет храниться db, независимо от того, где падает десятичная точка, а масштаб - это количество десятичных мест, которое будет хранить.

Поэтому нет необходимости перебирать свойства, как показано, но можно просто вызвать из

public class EFDbContext : DbContext
{
   protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
   {
       modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);

       base.OnModelCreating(modelBuilder);
   }
}
214
ответ дан AlexC 26 August 2018 в 02:22
поделиться

эта строка кода может быть более простым способом выполнить одно и то же:

 public class ProductConfiguration : EntityTypeConfiguration<Product>
    {
        public ProductConfiguration()
        {
            this.Property(m => m.Price).HasPrecision(10, 2);
        }
    }
12
ответ дан armadillo.mx 26 August 2018 в 02:22
поделиться

В EF6

modelBuilder.Properties()
    .Where(x => x.GetCustomAttributes(false).OfType<DecimalPrecisionAttribute>().Any())
    .Configure(c => {
        var attr = (DecimalPrecisionAttribute)c.ClrPropertyInfo.GetCustomAttributes(typeof (DecimalPrecisionAttribute), true).FirstOrDefault();

        c.HasPrecision(attr.Precision, attr.Scale);
    });
3
ответ дан Brian 26 August 2018 в 02:22
поделиться

Используя DecimalPrecisonAttribute из KinSlayerUY, в EF6 вы можете создать соглашение, которое будет обрабатывать отдельные свойства, которые имеют атрибут (в отличие от установки DecimalPropertyConvention, как в , этот ответ , который повлияет все десятичные свойства).

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
    public DecimalPrecisionAttribute(byte precision, byte scale)
    {
        Precision = precision;
        Scale = scale;
    }
    public byte Precision { get; set; }
    public byte Scale { get; set; }
}

public class DecimalPrecisionAttributeConvention
    : PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>
{
    public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)
    {
        if (attribute.Precision < 1 || attribute.Precision > 38)
        {
            throw new InvalidOperationException("Precision must be between 1 and 38.");
        }

        if (attribute.Scale > attribute.Precision)
        {
            throw new InvalidOperationException("Scale must be between 0 and the Precision value.");
        }

        configuration.HasPrecision(attribute.Precision, attribute.Scale);
    }
}

Затем в вашем DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
}
44
ответ дан Community 26 August 2018 в 02:22
поделиться

По-видимому, вы можете переопределить метод DbContext.OnModelCreating () и настроить такую ​​точность следующим образом:

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>().Property(product => product.Price).Precision = 10;
    modelBuilder.Entity<Product>().Property(product => product.Price).Scale = 2;
}

Но это довольно утомительный код, когда вы должны сделать это со всеми вашими ценами свойства, поэтому я придумал следующее:

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        var properties = new[]
        {
            modelBuilder.Entity<Product>().Property(product => product.Price),
            modelBuilder.Entity<Order>().Property(order => order.OrderTotal),
            modelBuilder.Entity<OrderDetail>().Property(detail => detail.Total),
            modelBuilder.Entity<Option>().Property(option => option.Price)
        };

        properties.ToList().ForEach(property =>
        {
            property.Precision = 10;
            property.Scale = 2;
        });

        base.OnModelCreating(modelBuilder);
    }

Хорошая практика, что вы вызываете базовый метод при переопределении метода, хотя базовая реализация ничего не делает.

Update : Эта статья также очень полезна.

44
ответ дан Dave Van den Eynde 26 August 2018 в 02:22
поделиться
[Column(TypeName = "decimal(18,2)")]

это будет работать с первыми миграциями кода, как описано здесь здесь .

1
ответ дан Elnoor 26 August 2018 в 02:22
поделиться

@ Mark007, я изменил критерии выбора типа для перемещения свойств DbSet & lt;> DbContext. Я думаю, что это безопаснее, потому что бывают случаи, когда у вас есть классы в данном пространстве имен, которые не должны быть частью определения модели, или они есть, но не являются сущностями. Или ваши объекты могут находиться в отдельных пространствах имен или отдельных сборках и объединяться в контекст.

Кроме того, даже если это маловероятно, я не думаю, что безопасно полагаться на упорядочение определений методов, поэтому лучше вытащите их с помощью списка параметров. (.GetTypeMethods () - это метод расширения, который я построил для работы с новой парадигмой TypeInfo и может сгладить иерархии классов при поиске методов).

Обратите внимание, что OnModelCreating делегирует этот метод:

    private void OnModelCreatingSetDecimalPrecisionFromAttribute(DbModelBuilder modelBuilder)
    {
        foreach (var iSetProp in this.GetType().GetTypeProperties(true))
        {
            if (iSetProp.PropertyType.IsGenericType
                    && (iSetProp.PropertyType.GetGenericTypeDefinition() == typeof(IDbSet<>) || iSetProp.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)))
            {
                var entityType = iSetProp.PropertyType.GetGenericArguments()[0];

                foreach (var propAttr in entityType
                                        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                        .Select(p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) })
                                        .Where(propAttr => propAttr.attr != null))
                {
                    var entityTypeConfigMethod = modelBuilder.GetType().GetTypeInfo().DeclaredMethods.First(m => m.Name == "Entity");
                    var entityTypeConfig = entityTypeConfigMethod.MakeGenericMethod(entityType).Invoke(modelBuilder, null);

                    var param = ParameterExpression.Parameter(entityType, "c");
                    var lambdaExpression = Expression.Lambda(Expression.Property(param, propAttr.prop.Name), true, new ParameterExpression[] { param });

                    var propertyConfigMethod =
                        entityTypeConfig.GetType()
                            .GetTypeMethods(true, false)
                            .First(m =>
                            {
                                if (m.Name != "Property")
                                    return false;

                                var methodParams = m.GetParameters();

                                return methodParams.Length == 1 && methodParams[0].ParameterType == lambdaExpression.GetType();
                            }
                            );

                    var decimalConfig = propertyConfigMethod.Invoke(entityTypeConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;

                    decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
                }
            }
        }
    }



    public static IEnumerable<MethodInfo> GetTypeMethods(this Type typeToQuery, bool flattenHierarchy, bool? staticMembers)
    {
        var typeInfo = typeToQuery.GetTypeInfo();

        foreach (var iField in typeInfo.DeclaredMethods.Where(fi => staticMembers == null || fi.IsStatic == staticMembers))
            yield return iField;

        //this bit is just for StaticFields so we pass flag to flattenHierarchy and for the purpose of recursion, restrictStatic = false
        if (flattenHierarchy == true)
        {
            var baseType = typeInfo.BaseType;

            if ((baseType != null) && (baseType != typeof(object)))
            {
                foreach (var iField in baseType.GetTypeMethods(true, staticMembers))
                    yield return iField;
            }
        }
    }
0
ответ дан Eniola 26 August 2018 в 02:22
поделиться

Вы всегда можете сказать EF об этом с условностями в классе Context в функции OnModelCreating следующим образом:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // <... other configurations ...>
    // modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    // modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    // modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

    // Configure Decimal to always have a precision of 18 and a scale of 4
    modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
    modelBuilder.Conventions.Add(new DecimalPropertyConvention(18, 4));

    base.OnModelCreating(modelBuilder);
}

Это относится только к Code First EF fyi и применяется ко всем десятичным типам, сопоставленным с db.

2
ответ дан Gecko IT 26 August 2018 в 02:22
поделиться

Вы можете найти дополнительную информацию о грани MSDN-модели данных сущностей. http://msdn.microsoft.com/en-us/library/ee382834.aspx Полностью рекомендуется.

1
ответ дан Jaider 26 August 2018 в 02:22
поделиться

Мне было приятно создать пользовательский атрибут для этого:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
    public DecimalPrecisionAttribute(byte precision, byte scale)
    {
        Precision = precision;
        Scale = scale;

    }

    public byte Precision { get; set; }
    public byte Scale { get; set; }

}

, используя его, как это

[DecimalPrecision(20,10)]
public Nullable<decimal> DeliveryPrice { get; set; }

, и магия возникает при создании модели с некоторым отражением

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
    foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalPrecisionAttribute)).GetTypes()
                                   where t.IsClass && t.Namespace == "YOURMODELNAMESPACE"
                                   select t)
     {
         foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<DecimalPrecisionAttribute>() != null).Select(
                p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) }))
         {

             var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
             ParameterExpression param = ParameterExpression.Parameter(classType, "c");
             Expression property = Expression.Property(param, propAttr.prop.Name);
             LambdaExpression lambdaExpression = Expression.Lambda(property, true,
                                                                      new ParameterExpression[]
                                                                          {param});
             DecimalPropertyConfiguration decimalConfig;
             if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
             {
                 MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
                 decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
             }
             else
             {
                 MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
                 decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
             }

             decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
        }
    }
}

первая часть состоит в том, чтобы получить все классы в модели (мой пользовательский атрибут определен в этой сборке, поэтому я использовал это, чтобы получить сборку с моделью)

второй foreach получает все свойства этого класса с помощью настраиваемого атрибута и самого атрибута, поэтому я могу получить данные о точности и масштабировании

после этого, я должен вызвать

modelBuilder.Entity<MODEL_CLASS>().Property(c=> c.PROPERTY_NAME).HasPrecision(PRECISION,SCALE);

, поэтому i вызовите modelBuilder.Entity () отражением и сохраните его в переменной entityConfig, после чего я создаю lambda выражение c => c.PROPERTY_NAME

. После этого, если десятичное значение является нулевым, я вызываю

Property(Expression<Func<TStructuralType, decimal?>> propertyExpression) 

метод (я называю это положением в массиве, он не идеален, я знаю, любая помощь будет очень оценена)

, и если она не может быть нулевой, я вызываю

Property(Expression<Func<TStructuralType, decimal>> propertyExpression)

.

Наличие DecimalPropertyConfiguration i вызывает Метод HasPrecision.

66
ответ дан Jess 26 August 2018 в 02:22
поделиться

Если вы хотите установить точность для всех decimals в EF6, вы можете заменить стандартное соглашение DecimalPropertyConvention, используемое в DbModelBuilder:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
    modelBuilder.Conventions.Add(new DecimalPropertyConvention(38, 18));
}

По умолчанию DecimalPropertyConvention в EF6 отображает свойства decimal в столбцы decimal(18,2).

Если вы хотите, чтобы отдельные свойства имели заданную точность, вы можете установить точность для свойства объекта в DbModelBuilder:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyEntity>().Property(e => e.Value).HasPrecision(38, 18);
}

Или добавьте EntityTypeConfiguration<> для объекта, который задает точность:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new MyEntityConfiguration());
}

internal class MyEntityConfiguration : EntityTypeConfiguration<MyEntity>
{
    internal MyEntityConfiguration()
    {
        this.Property(e => e.Value).HasPrecision(38, 18);
    }
}
66
ответ дан kjbartel 26 August 2018 в 02:22
поделиться

Пользовательский атрибут KinSlayerUY работал хорошо для меня, но у меня были проблемы с ComplexTypes. Они были сопоставлены как сущности в коде атрибута, поэтому не могли затем отображаться как ComplexType.

Поэтому я расширил код, чтобы это можно было сделать:

public static void OnModelCreating(DbModelBuilder modelBuilder)
    {
        foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalPrecisionAttribute)).GetTypes()
                                   where t.IsClass && t.Namespace == "FA.f1rstval.Data"
                                   select t)
        {
            foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<DecimalPrecisionAttribute>() != null).Select(
                   p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) }))
            {

                ParameterExpression param = ParameterExpression.Parameter(classType, "c");
                Expression property = Expression.Property(param, propAttr.prop.Name);
                LambdaExpression lambdaExpression = Expression.Lambda(property, true,
                                                                         new ParameterExpression[] { param });
                DecimalPropertyConfiguration decimalConfig;
                int MethodNum;
                if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    MethodNum = 7;
                }
                else
                {
                    MethodNum = 6;
                }

                //check if complextype
                if (classType.GetCustomAttribute<ComplexTypeAttribute>() != null)
                {
                    var complexConfig = modelBuilder.GetType().GetMethod("ComplexType").MakeGenericMethod(classType).Invoke(modelBuilder, null);
                    MethodInfo methodInfo = complexConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[MethodNum];
                    decimalConfig = methodInfo.Invoke(complexConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
                }
                else
                {
                    var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
                    MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[MethodNum];
                    decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
                }

                decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
            }
        }
    }
0
ответ дан Mark007 26 August 2018 в 02:22
поделиться

Entity Framework Ver 6 (Alpha, rc1) имеет нечто вроде пользовательских условных обозначений. Чтобы установить десятичную точность:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Properties<decimal>().Configure(config => config.HasPrecision(18, 4));
}

Ссылка:

28
ответ дан nhahtdh 26 August 2018 в 02:22
поделиться

Использование

System.ComponentModel.DataAnnotations;

Вы можете просто поместить этот атрибут в свою модель:

[DataType("decimal(18,5)")]
2
ответ дан VinnyG 26 August 2018 в 02:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: