我一直在运行这个存储过程一段时间使用LINQ to SQL将它映射到我的对象,现在,突然之间,LINQ试图将字段'value'强制转换为double。
错误:
specified cast is not valid
System.Data.SqlClient.SqlBuffer.get_Double()
System.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)
Read_Field(ObjectMaterializer`1 )
System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
CmsCsharp.BL.FieldManager.FindContentField(Int32 id_content, Int32 id_fieldType, String fieldName)
方法
public static Field FindContentField(int id_content, int id_fieldType, string fieldName)
{
using (DbBase conn = new DbBase())
{
try
{
return conn.ClientDB.sprocFindContentField(id_content, id_fieldType, fieldName).Single();
}
catch (Exception e)
{
return null;
}
}
}
[Function(Name = "dbo.sprocFindContentField")]
public ISingleResult<Field> sprocFindContentField([Parameter(DbType="Int")] System.Nullable<int> id_content, [Parameter(DbType="Int")]System.Nullable<int> id_fieldType, [Parameter(DbType="Varchar(255)")]string fieldName)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id_content, id_fieldType, fieldName);
return ((ISingleResult<Field>)(result.ReturnValue));
}
字段:
public class TextField : Field
{
private string _Value;
[Column(Storage="_Value", DbType="VarChar(MAX) NOT NULL", Name="value", CanBeNull=true)]
public string Value
{ //get and set }
}
程序:
SELECT
type = 'textField',
fieldLabel.id_fieldEditor,
fieldLabel.id_fieldType ,
fieldLabel.description,
fieldLabel.id ,
sessionField.id AS relationId,
fieldLabel.name ,
fieldText.content AS value,
fieldText.id AS valueId,
cf.id as contentRelationId
FROM
fieldLabel
INNER JOIN
sessionField
ON
sessionField.id_fieldLabel = fieldLabel.id
INNER JOIN
cms_contentField AS cf
ON
cf.id_sessionField = sessionField.id
INNER JOIN
fieldText
ON
fieldText.id = cf.id_fieldValue
INNER JOIN
fieldType
ON
fieldLabel.id_fieldType = fieldType.id
INNER JOIN
fieldTable
ON
fieldTable.id = fieldType.id_fieldTable
WHERE
cf.id_content = @id_content
AND
fieldTable.id = 1
答案 0 :(得分:1)
我的猜测是你的表中有一个值,它通过“数字”数据类型触发SQL Server的转换。即您的值被转换为不精确的数据类型(浮点数或实数)并导致LINQ to SQL尝试双精度数。请参阅SQL Server's help file for int
中的注意事项答案 1 :(得分:0)
我已将存储过程中的第9行更改为 fieldText.content,
SELECT
type = 'textField',
fieldLabel.id_fieldEditor,
fieldLabel.id_fieldType ,
fieldLabel.description,
fieldLabel.id ,
sessionField.id AS relationId,
fieldLabel.name ,
fieldText.content AS value, //this line
fieldText.id AS valueId,
cf.id as contentRelationId
然后我相应地更改了映射:
[Column(Storage="_Value", DbType="VarChar(MAX) NOT NULL", Name="content", CanBeNull=true)]
public string Value
{ //get and set }
它有效!
不知道为什么会有效。标签(作为价值)可能是原因吗?
谢谢
答案 2 :(得分:0)
我在相同的情况下使用相同的错误消息迟到了。
问题是我的Proc Stock结果中的一个未命名的列。在管理工作室执行它没问题。在我的dbml中注册它没问题,但在运行时无效。
希望有所帮助