我在 EntityFramework(模型优先)中保存小数时遇到问题。 在我的 EDMX 中,我声明我的属性为 Decimal(30,10),然后我尝试保存该数字: '1215867935736100000'
结果是:
Parameter value '1215867935736100000' is out of range.
我尝试使用 SQL Server 2008 R2 Management Studio 进行 INSERT 查询
INSERT INTO MyTable (MyColumn) VALUES (1215867935736100000)
并且记录已正确保存。
你有什么想法吗?
谢谢, 最大
最佳答案
这可能是您使用的 EF 版本存在问题。
我有一个问题的代码优先版本,几乎可以重现您遇到的错误。我收到的错误消息中包含 .00
{"Parameter value '1215867935736100000.00' is out of range."}
我解决这个问题的方法是将.HasPrecision(30,10)
添加到EntityTypeConfiguration
类中。
this.Property(t => t.value)
.HasColumnName("value")
.HasPrecision(30,10) //this line resolves the problem in my test code
.IsRequired();
这并不能解决您的问题,但我至少可以确认 Entity Framework 可以将值 1215867935736100000
写入数据库。
CREATE TABLE [dbo].[TestTable1](
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [decimal](30, 10) NOT NULL
) ON [PRIMARY]
测试代码:
MyContext testTableContext = new MyContext();
TestTable1 testTable1 = new TestTable1();
testTable1.value = 1215867935736100000;
testTableContext.TestTable1.Add(testTable1);
testTableContext.SaveChanges();
关于c# - Entity Framework : "Parameter value is out of range.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20264558/