EF 4.1 使用自定义类型处理资金
我一直在 POCO 中使用 Money 的自定义类型,并尝试将其插入到我的数据库中,但它被实体框架隐式丢弃。
这是我的简单代码;
我的类型;
public struct Money
{
private static decimal _value;
public Money(Decimal value)
{
_value = value;
}
public static implicit operator Money(Decimal value)
{
return new Money(value);
}
public static implicit operator decimal(Money value)
{
return _value;
}
}
我的对象;
public class MyObject
{
[Key]
public int Id { get; set; }
public Money MyMoney { get; set; }
}
我的背景;
public class Data : DbContext
{
public Data()
: base("Data Source=.;Database=MyTest;Integrated Security=True")
{}
public DbSet<MyObject> MyObject { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyObject>()
.Property(p => p.MyMoney).HasColumnName("MyMoney");
}
}
当我使用此代码时,出现以下错误。
属性“MyMoney”不是 类型“MyObject”上声明的属性。 确认该房产没有被 明确从模型中排除 使用忽略方法或 NotMappedAttribute 数据注释。 确保它是一个有效的原语 财产。
我猜问题出在最后一句......那么,什么是有效的原始属性?还有其他方法可以解决这个问题吗?
I have been using a custom type for Money with my POCOs and tries to insert this to my database but it is implicitly thrown away by Entity Framework.
This is my code made simple;
My type;
public struct Money
{
private static decimal _value;
public Money(Decimal value)
{
_value = value;
}
public static implicit operator Money(Decimal value)
{
return new Money(value);
}
public static implicit operator decimal(Money value)
{
return _value;
}
}
My object;
public class MyObject
{
[Key]
public int Id { get; set; }
public Money MyMoney { get; set; }
}
My context;
public class Data : DbContext
{
public Data()
: base("Data Source=.;Database=MyTest;Integrated Security=True")
{}
public DbSet<MyObject> MyObject { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyObject>()
.Property(p => p.MyMoney).HasColumnName("MyMoney");
}
}
When I use this code I get the following error.
The property 'MyMoney' is not a
declared property on type 'MyObject'.
Verify that the property has not been
explicitly excluded from the model by
using the Ignore method or
NotMappedAttribute data annotation.
Make sure that it is a valid primitive
property.
I guess the problem is the last sentence.... Then, what is a valid primitive property? Is there any other way to take care of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过显式映射十进制属性并仅向调用者公开
Money
属性来使用它:You could take of it by explicitly mapping a decimal property and only exposing the
Money
Property to callers:好吧,这就是我的解决方案。
为了能够读取该私有属性,我创建了一个属性扩展,如下所示。由于我的一些 Money 类型的属性是可为空的,所以我也必须处理这个问题。
然后我可以用它来读取我的私有财产。
谢谢史蒂夫带我走向正确的方向。
Ok, this went to be my solution.
To be able to read that private property I created a property extension as below. Since some of my properties of type Money is Nullable I had to take care of that as well.
And then I could use that to read my private property.
Thank you Steve for taking me in the right direction.
你也可以在复杂类中改变你的“金钱”;这将使公开的属性在您的数据库中内嵌。仅使用 Decimal 属性,它看起来像 Money_Value (假定您将 Value 作为属性公开)。假设您将“Currency”作为字符串添加到类中。然后数据库中也会有 Money_Currency。
要向 EF 声明您的类是 Complex 类型,只需使用 [ComplexType()] 对其进行注释即可。
为了充分利用 Struct + ComplexType,您甚至可以同时使用两者:复杂类型在内部使用结构(ComplexType 只是一个包装器)。
You could also have transformed your "Money" in a Complex class; which would make the exposed properties in-line in your database. With only the Decimal property, it would look like Money_Value (given you expose Value as a property). Let's say you add "Currency" as a string to the class. You would then have also Money_Currency in the database.
To declare to EF that your class is a Complex type, just annotate it with [ComplexType()].
To get the best of Struct + ComplexType, you could even use both at the time : the complex type using the struct internally (the ComplexType just being a wrapper).