实体框架:“参数值超出范围”。 [英] EntityFramework: "Parameter value is out of range."

查看:459
本文介绍了实体框架:“参数值超出范围”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EntityFramework(Model First)中保存十进制的问题。
在我的EDMX中,我宣布我的支持为十进制(30,10),然后我试图保存号码:
'1215867935736100000'



结果是:

 参数值'1215867935736100000'超出范围。 

我尝试使用SQL Server 2008 R2 Management Studio进行INSERT查询

  INSERT INTO MyTable(MyColumn)VALUES(1215867935736100000)

,记录保存正确。



你有什么想法吗?



谢谢,
最大

解决方案

这可能是您正在使用的EF版本的问题。

我有一个代码第一个版本的问题,几乎可以重现您所获得的错误。我得到的错误包括消息中的.00。


{参数值1215867935736100000.00超出范围。}


我解决的方法是将 .HasPrecision(30,10)添加到 EntityTypeConfiguration 类。

  this.Property(t => t.value)
.HasColumnName(value)
.HasPrecision(30,10)//这行解决了我的测试代码
.IsRequired()的问题;

这不会解决您的问题,但至少可以确认实体框架可以写$ 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();


I have a problem with saving a decimal in EntityFramework (Model First). In my EDMX I declared my proprierty as a Decimal(30,10) then I tried to save the number: '1215867935736100000'

The result is:

Parameter value '1215867935736100000' is out of range.

I tried to make an INSERT query with SQL Server 2008 R2 Management Studio

INSERT INTO MyTable (MyColumn) VALUES (1215867935736100000)

and the record is saved correctly.

Do you have any idea?

Thanks, Max

解决方案

This could be an issue with the version of EF that you are using.

I have a code first version of the problem and can almost reproduce the error you are getting. The error I get includes a .00 in the message

{"Parameter value '1215867935736100000.00' is out of range."}

The way I solved it was to add .HasPrecision(30,10) to the EntityTypeConfiguration class.

this.Property(t => t.value)
    .HasColumnName("value")
    .HasPrecision(30,10) //this line resolves the problem in my test code
    .IsRequired();

This doesn't fix your problem but I can at least confirm it is possible for Entity Framework to write the value 1215867935736100000 to the database.

CREATE TABLE [dbo].[TestTable1](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [value] [decimal](30, 10) NOT NULL
) ON [PRIMARY]

The test code:

MyContext testTableContext = new MyContext();
TestTable1 testTable1 = new TestTable1();
testTable1.value = 1215867935736100000;
testTableContext.TestTable1.Add(testTable1);
testTableContext.SaveChanges();

这篇关于实体框架:“参数值超出范围”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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