【发布时间】:2012-12-30 22:38:29
【问题描述】:
MySql 和 Entity Framework Code First 有问题。
web.config.xml 中的我的连接字符串
<add name="EntityContext" connectionString="Server=127.0.0.1; port=8080 Database=simple_crud; Uid=root; Pwd=;" providerName="MySql.Data.MySqlClient" />
我的背景:
public class EntityContext : DbContext
{
public DbSet<Pessoa> Pessoas { get; set; }
}
我的班级:
[Table("pessoa")]
public class Pessoa
{
[Key]
[Column("Id")]
public int Id { get; set; }
[Column("Nome")]
public string Nome { get; set; }
}
我的控制器
public EntityContext db = new EntityContext();
public ActionResult Index()
{
List<Pessoa> pessoas = db.Pessoas.ToList(); // here is the error
return View(pessoas);
}
所以,当我按 F5 时,我得到了这个:“输入字符串的格式不正确”。
我想念什么?
更新
堆栈跟踪:
[FormatException:输入字符串的格式不正确。]
System.Number.StringToNumber(String str, NumberStyles 选项, NumberBuffer& 数字, NumberFormatInfo 信息, Boolean parseDecimal) +9591147 System.Number.ParseUInt32(字符串值,NumberStyles 选项,NumberFormatInfo numfmt)+119
System.String.System.IConvertible.ToUInt32(IFormatProvider 提供程序) +46 System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +9509065
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.ChangeType(对象 值,类型 t) +240
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.SetValue(字符串 关键字,对象值)+399
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item(字符串 关键字,对象值)+54
System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(字符串 价值)+127
MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(字符串 价值)+289
System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(字符串 姓名)+409
System.Data.Entity.Internal.LazyInternalConnection.Initialize() +49
System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() +10 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +265 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型 entityType) +17
System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +621.GetEnumerator() +15
System.Data.Entity.Internal.Linq.InternalSet
System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable1 来源)+58 SimpleMysqlCrud.Controllers.PessoaController.Index() 在 f:\users\pablo.rocha.fti\documents\visual studio 2010\Projects\SimpleMysqlCrud\SimpleMysqlCrud\Controllers\PessoaController.cs:16 lambda_method(闭包, ControllerBase, Object[]) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext 控制器上下文,IDictionary2 parameters) +2082 参数)+27
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.c_DisplayClass15.b_12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func1 continuation) +2631 过滤器,ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList2 parameters) +1911.b__7(IAsyncResult ) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.c_DisplayClasse.b_d() +50
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8
System.Web.Mvc.SecurityUtil.b_0(动作 f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作动作) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970141 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
【问题讨论】:
-
哪一行出现异常?
-
你忘了在port=8080后面加分号吗?
-
@AndreyVoloshin,请给我一个例子。
-
@DStanley,例外在这里:db.Pessoas.ToList()
-
请同时发布堆栈跟踪。
标签: c# mysql frameworks entity