Linq to Sql,System.Exception 强制转换无效
我正在尝试将一个类从 SQL 映射到 linq 集合,但我失败了..
我有这个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace SportsStore.Entities
{
[Table(Name = "Products")]
public class Product
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int ProductID { get; set; }
[Column]public string Name { get; set; }
[Column]public string Description { get; set; }
[Column]public `int` Price { get; set; }
[Column]public string Category { get; set; }
}
}
我从另一个类调用该类:
using SportsStore.Abstract;
using SportsStore.Entities;
using System.Data.Linq;
using System.Linq;
namespace SportsStore.Concrete
{
public class SqlProductsRepository : IProductRepository
{
private Table<Product> productsTable;
public SqlProductsRepository(string connnectionString)
{
productsTable = (new DataContext(connnectionString)).GetTable<Product>();
}
public IQueryable<Product> Products
{
get { return productsTable; }
}
}
}
基本上,我得到了这个:
base {System.SystemException} = {“指定的转换无效。”}
这意味着转换无效
更新 我的数据库看起来像这样:
>
**ProductID int (primary key) Name
nvarchar(100) Description
nvarchar(500) Category
nvarchar(50) Price int**
堆栈跟踪:
[InvalidCastException:指定的转换无效。]
System.Data.SqlClient.SqlBuffer.get_Decimal() +274
System.Data.SqlClient.SqlDataReader.GetDecimal(Int32 i) +44
Read_Product(ObjectMaterializer<代码>1) +1088
System.Data.Linq.SqlClient.ObjectReader2.MoveNext() +32
System.Collections.Generic.List1..ctor(IEnumerable
1 集合) +406 System.Linq.Enumerable.ToList(IEnumerable1 源) +58
2 个参数)+409
SportsStore.Controllers.ProductController.List() 中 D:\Call.of.Duty.Modern.Warfare.3-RELOADED\SportsStore\SportsStore\SportsStore\Controllers\ProductController.cs:28 lambda_method(闭包,ControllerBase,对象[])+96
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,对象[]参数)+51
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext、ActionDescriptor actionDescriptor、IDictionary2 参数)+52
1 延续)+436
System.Web.Mvc.<>c__DisplayClassd.b__a() +127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func
System.Web.Mvc.<>c_DisplayClassf.b_c() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext、IList1 过滤器、ActionDescriptor actionDescriptor、 IDictionary
2 个参数)+305
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext 控制器上下文,字符串操作名称)+830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext 请求上下文)+39
System.Web.Mvc.<>c_DisplayClass8.b_4() +65
System.Web.Mvc.Async。<>c_DisplayClass1.b_0() +44
System.Web.Mvc.Async.<>c__DisplayClass81.
1.End() +141 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+54b__7(IAsyncResult _) +42 System.Web.Mvc.Async.WrappedAsyncResult
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果)+38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8966925 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,Boolean&completedSynchronously) +184
I am trying to map a class from SQL to a linq collection, But I fail..
I have got this class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace SportsStore.Entities
{
[Table(Name = "Products")]
public class Product
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int ProductID { get; set; }
[Column]public string Name { get; set; }
[Column]public string Description { get; set; }
[Column]public `int` Price { get; set; }
[Column]public string Category { get; set; }
}
}
I call that class from another class:
using SportsStore.Abstract;
using SportsStore.Entities;
using System.Data.Linq;
using System.Linq;
namespace SportsStore.Concrete
{
public class SqlProductsRepository : IProductRepository
{
private Table<Product> productsTable;
public SqlProductsRepository(string connnectionString)
{
productsTable = (new DataContext(connnectionString)).GetTable<Product>();
}
public IQueryable<Product> Products
{
get { return productsTable; }
}
}
}
Basically, I get this:
base {System.SystemException} = {"Specified cast is not valid."}
This implies the cast is invalid
update
My database looks like that:
>
**ProductID int (primary key) Name
nvarchar(100) Description
nvarchar(500) Category
nvarchar(50) Price int**
StackTrace:
[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Decimal() +274
System.Data.SqlClient.SqlDataReader.GetDecimal(Int32 i) +44
Read_Product(ObjectMaterializer1 ) +1088
2.MoveNext() +32
System.Data.Linq.SqlClient.ObjectReader
System.Collections.Generic.List1..ctor(IEnumerable
1 collection) +406
System.Linq.Enumerable.ToList(IEnumerable1 source) +58
2 parameters) +409
SportsStore.Controllers.ProductController.List() in
D:\Call.of.Duty.Modern.Warfare.3-RELOADED\SportsStore\SportsStore\SportsStore\Controllers\ProductController.cs:28
lambda_method(Closure , ControllerBase , Object[] ) +96
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
controller, Object[] parameters) +51
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary2
1 continuation) +436
parameters) +52
System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
+127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
filter, ActionExecutingContext preContext, Func
System.Web.Mvc.<>c_DisplayClassf.b_c()
+61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList1 filters, ActionDescriptor actionDescriptor,
2 parameters) +305
IDictionary
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName) +830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
+111 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext
requestContext) +39
System.Web.Mvc.<>c_DisplayClass8.b_4() +65
System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +44
System.Web.Mvc.Async.<>c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult
1.End() +141 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
_) +42 System.Web.Mvc.Async.WrappedAsyncResult
Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
+52 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+8966925 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的错误消息表明
Price
列的值无法转换为小数
。您需要确保数据库表的
Price
列具有正确的列类型,并且包含有效数据。另外,如果数据库中的列允许 NULL 值,则需要使用
decimal? 将其映射到可为空的小数点?价格{获取;放; }
Your error message states that the value for the
Price
column cannot be converted to adecimal
.You need to make sure that your database table has the correct column type for the
Price
column and that it contains valid data.Also, if your column in the database allows NULL values, you need to map it to a nullable decimal with
decimal? Price { get; set; }
您有一个定义为
[Column]public Decimal Price { get; 的列放; }
。您确定这实际上是适合数据库中数据的数据类型吗? - 数据库中的数据可以是Varchar
,还是允许空值?You've got a column defined as
[Column]public decimal Price { get; set; }
. Are you sure this is actually a suitable datatype for the data in the database? - could the data in the database beVarchar
, or allow nulls?