gpt4 book ai didi

sql - Linq 到 Sql,System.Exception 转换无效

转载 作者:行者123 更新时间:2023-12-04 14:09:09 36 4
gpt4 key购买 nike

我试图将一个类从 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; }

}
}

我从另一个类(class)调用该类(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; }
}
}

}

基本上,我得到了这个:

base {System.SystemException} = {"Specified cast is not valid."}



这意味着类型转换无效

更新
我的数据库看起来像这样:

>
 **ProductID  int (primary key) Name  
nvarchar(100) Description
nvarchar(500) Category
nvarchar(50) Price int**

堆栈跟踪:

[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

System.Data.Linq.SqlClient.ObjectReader
2.MoveNext() +32
System.Collections.Generic.List1..ctor(IEnumerable1 collection) +406 System.Linq.Enumerable.ToList(IEnumerable1 source) +58

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
2 parameters) +409
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2
parameters) +52

System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
+127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
filter, ActionExecutingContext preContext, Func
1 continuation) +436
System.Web.Mvc.<>c_DisplayClassf.b_c() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor,
IDictionary
2 parameters) +305
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
_) +42 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +141 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 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

最佳答案

您的错误消息指出 Price 的值列无法转换为 decimal .

您需要确保您的数据库表具有适用于 Price 的正确列类型。列并且它包含有效数据。

此外,如果数据库中的列允许 NULL 值,则需要使用 decimal? Price { get; set; } 将其映射到可为空的小数点。

关于sql - Linq 到 Sql,System.Exception 转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8788188/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com