提交 34e3a149 编写于 作者: S Shay Rojansky

Fix mapping of PG money

Fixes #1467

(cherry picked from commit a213f868)
上级 ad4abdb0
using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore.Storage;
using NpgsqlTypes;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping
{
public class NpgsqlMoneyTypeMapping : DecimalTypeMapping
{
public NpgsqlMoneyTypeMapping() : base("money", System.Data.DbType.Currency) {}
protected NpgsqlMoneyTypeMapping(RelationalTypeMappingParameters parameters)
: base(parameters)
{
}
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new NpgsqlMoneyTypeMapping();
protected override string GenerateNonNullSqlLiteral(object value)
=> base.GenerateNonNullSqlLiteral(value) + "::money";
}
}
......@@ -34,7 +34,7 @@ public class NpgsqlTypeMappingSource : RelationalTypeMappingSource
readonly FloatTypeMapping _float4 = new FloatTypeMapping("real", DbType.Single);
readonly DoubleTypeMapping _float8 = new DoubleTypeMapping("double precision", DbType.Double);
readonly DecimalTypeMapping _numeric = new DecimalTypeMapping("numeric", DbType.Decimal);
readonly DecimalTypeMapping _money = new DecimalTypeMapping("money");
readonly NpgsqlMoneyTypeMapping _money = new NpgsqlMoneyTypeMapping();
readonly GuidTypeMapping _uuid = new GuidTypeMapping("uuid", DbType.Guid);
readonly ShortTypeMapping _int2 = new ShortTypeMapping("smallint", DbType.Int16);
readonly ByteTypeMapping _int2Byte = new ByteTypeMapping("smallint", DbType.Byte);
......
......@@ -459,7 +459,7 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types()
@p8='2016-01-02T11:11:12' (DbType = DateTimeOffset)
@p9='0001-01-01T12:00:00.0000000+02:00' (DbType = Object)
@p10='101.7'
@p11='81.1'
@p11='81.1' (DbType = Currency)
@p12='103.9'
@p13='System.Collections.Generic.Dictionary`2[System.String,System.String]' (Nullable = false) (DbType = Object)
@p14='85.5'
......@@ -869,7 +869,7 @@ public void Sum_Conversions()
{
using var context = CreateContext();
// PostgreSQL SUM() returns numeric for bigint input, bigint for int/smallint inuts.
// PostgreSQL SUM() returns numeric for bigint input, bigint for int/smallint ints.
// Make sure the proper conversion is done
var sum1 = context.Set<MappedDataTypes>().Sum(m => m.LongAsBigint);
var sum2 = context.Set<MappedDataTypes>().Sum(m => m.Int);
......@@ -886,6 +886,23 @@ public void Sum_Conversions()
FROM ""MappedDataTypes"" AS m");
}
[ConditionalFact]
public void Money_compare_constant()
{
using var context = CreateContext();
_ = context.Set<MappedDataTypes>().Where(m => m.DecimalAsMoney > 3).ToList();
}
[ConditionalFact]
public void Money_compare_parameter()
{
using var context = CreateContext();
var money = 3m;
_ = context.Set<MappedDataTypes>().Where(m => m.DecimalAsMoney > money).ToList();
}
void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
......
......@@ -27,20 +27,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Order>()
.Property(o => o.EmployeeID)
.HasColumnType("int4");
modelBuilder.Entity<OrderDetail>()
.Property(od => od.UnitPrice)
.HasColumnType("money");
modelBuilder.Entity<Product>(
b =>
{
b.Property(p => p.UnitPrice).HasColumnType("money");
b.Property(p => p.UnitsInStock).HasColumnType("int2");
});
modelBuilder.Entity<MostExpensiveProduct>()
.Property(p => p.UnitPrice)
.HasColumnType("money");
modelBuilder.Entity<Product>(b => b.Property(p => p.UnitsInStock).HasColumnType("int2"));
#pragma warning disable CS0618 // Type or member is obsolete
modelBuilder.Query<CustomerView>().HasNoKey().ToQuery(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册