实体框架以磅计 [英] EntityFramework pence to pounds

查看:348
本文介绍了实体框架以磅计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个WPF应用程序。我使用Code-First制作了我的数据库。



我需要在便士(例如500)中插入物品的价格,但是当价格显示时,它们应该是磅( 5,00)。



我试图在我的财产的设定者中进行转换

  public decimal价格
{
get {return price; }
set {price = value / 100;}
}

但是,由于某些原因,当我手动删除数据库时,我的 ListView 中的值显示为0,05,其他时间像500,00。这个方法一定是错的。



如何告诉我的数据库考虑我的值的最后2位数为十进制?

解决方案

你可能想要尝试这样的一个

  public class YourClassName {
public decimal Price {get;组; }

[NotMapped]
public decimal Pounds =>价格/ 100;
}


I'm building a WPF application. I made my database using Code-First.

I need to insert prices of items in pence (e.g. 500), but when prices will be displayed they should be in pounds (5,00).

I tried to do the "conversion" in the setter of my property

public decimal Price
{
   get { return price; }
   set { price = value/100 ;}
}

But, for some reason, when I drop manually the database, the values in my ListView are shown some times like 0,05 and other times like 500,00. Something must be wrong with this method.

How can I tell my database to consider last 2 numbers of my value as decimal?

解决方案

You might want to try something like this

public class YourClassName {
   public decimal Price { get; set; }

   [NotMapped]
   public decimal Pounds => Price/100;
}

这篇关于实体框架以磅计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆