我们有一个Price值对象,我们希望通过Linq-to-Sql映射到十进制SQL字段。
我们直接使用Linq-to-SQL的属性(即......我们不使用Linq-to-Sql设计器)。
我想写这样的东西:
[Table]
public class Product: Aggregate
{
//...
[Column]
public string Name {get; set;}
[Column]
public Price Price {get; set;}
[Column]
public Price? GoldCustomerPrice {get; set;}
//...
}
在上面的示例中,Linq-to-sql自动检测到Name应该映射到VarChar。但是我如何告诉它将Price.ExcludingVat映射到十进制并将GoldCustomerPrice.Value.ExcludingVat映射到允许为null的十进制字段?
这就是我现在这样做的方式。非常详细。
[Table]
public class Product: Aggregate
{
//...
[Column]
public string Name {get; set;}
[Column(Name = "Price")]
decimal priceExcludingVat;
public Price Price
{
get { return new Price(priceExcludingVat) }
set { priceExcludingVat = value.ExcludingVat ; }
}
[Column(Name = "GoldCustomerPrice")]
private decimal? goldCustomerPriceExcludingVat;
public Price? GoldCustomerPrice
{
get
{
if(goldCustomerPriceExcludingVat.HasValue)
return new Price(goldCustomerPriceExcludingVat.Value)
else
return (Price?) null;
}
set
{
if (value == null)
goldCustomerPriceExcludingVat = null;
else
goldCustomerPriceExcludingVat = value.Value.ExcludingVat;
}
}
//...
}
如果您想知道Price值对象是这样的:
public struct Price
{
public Price(decimal priceExcludingVat) : this()
{
ExcludingVat = priceExcludingVat;
VatRate = Settings.VatRate;
}
public decimal ExcludingVat { get; private set; }
public decimal VatRate{ get; set;}
public decimal Vat
{
get { return ExcludingVat * VatRate; }
}
public decimal IncludingVat
{
get { return ExcludingVat * Vat; }
}
//... a lot of operator overloads
}
答案 0 :(得分:0)
这就是你要追求的吗?
[Column(Storage="Price", DbType="float NOT NULL")]
答案 1 :(得分:0)
它是否适用于运算符重载,在哪里实现小数?