我的模型:
public class Product
{
...
public decimal Fineness { get; set; }
...
}
播种数据库:
new List<Product>
{
new Product { ..., Fineness = 0.757M, ... },
new Product { ..., Fineness = 0.674M, ... },
new Product { ..., Fineness = 0.475M, ... }
}.ForEach(p => context.Products.Add(p));
查询数据库以测试播种:
var products = db.Products.ToList();
foreach (var p in products)
{
S.D.Debug.WriteLine("ProductList: {0}, {1}", p.Name, p.Fineness);
}
控制台输出:
ProductList: Test Product, 0.75
ProductList: Test Product, 0.67
ProductList: Test Product, 0.47
我是不是在做一些很傻的事情???所有内容都被截断到小数点后两位。
解决方案 - 感谢 Patrick:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Property(x => x.Fineness).HasPrecision(10, 5);
}
最佳答案
所以您已经定义了标准实体模型,这里是带有 id 和 decimal 的产品,以及您需要的任何其他内容等。
public class Product
{
public int Id { get; set; }
public decimal Fineness { get; set; }
}
所以我定义了一个初始化程序,在这种情况下,数据库将删除并重新创建我提供的任何种子信息,每次我运行和执行我的应用程序时,都会调用它。
public class Initializer : DropCreateDatabaseAlways<Context>
{
protected override void Seed(Context context)
{
// note how I am specifying it here as 4 digits after the decimal point
// and for the second one, 3 digits
// this is where EF precision must be configured so you can expect
// the values you tell EF to save to the db
context.Products.Add(new Product() {Id = 1, Fineness = 145.2442m});
context.Products.Add(new Product() {Id = 2, Fineness = 12.341m});
}
}
public class Context : DbContext
{
public IDbSet<Product> Products { get; set; }
public Context()
{
// I always explicitly define how my EF should run, but this is not needed for the answer I am providing you
Configuration.AutoDetectChangesEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// so here, I am override the model configuration which is what
// EF can use in order to set-up the behaviour of how everything
// is configured in the database, from associations between
// multiple entities and property validation, Null-able, Precision, required fields etc
modelBuilder.Configurations.Add(new ProductConfiguration());
}
}
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
ToTable("Product");
HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// HAS PRECISION.
// Enforces how the value is to be stored in the database
// Here you can see I set a scale of 3, that's 3 digits after
// the decimal. Notice how in my seed method, I gave a product 4 digits!
// That means it will NOT save the product with the other trailing digits.
Property(x => x.Fineness).HasPrecision(precision: 10, scale: 3);
}
}
使用 SQL Server 对象资源管理器,我可以查看我制作的 localdb 示例产品,以了解 EF 如何配置我的数据库。
[TestFixture]
public class Tests
{
[Test]
public void Test()
{
Database.SetInitializer(new Initializer());
using (var ctx = new Context())
{
// assert our findings that it is indeed not what we actually specified in the seed method, because of our Entity configuration with HasPrecision.
Product product1 = ctx.Products.Find(1);
Assert.AreEqual(145.244m, product1.Fineness);
Product product2 = ctx.Products.Find(2);
Assert.AreEqual(12.341m, product2.Fineness);
}
}
}
所以我们需要确保数据库知道它应该如何存储我们的十进制值,通过使用 Entity Framework 的模型构建器配置来配置我们的实体,通过使用 FluentApi ,我们可以通过 EntityTypeConfiguration<T> 设置属性特征.
关于c# - 不需要的十进制截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16929388/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night