2013-01-16 226 views
3

我有一个MySql和实体框架代码优先的问题。MySql和实体框架代码优先

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); 
} 
在web.config.xml

<add name="EntityContext" connectionString="Server=127.0.0.1; port=8080 Database=simple_crud; Uid=root; Pwd=;" providerName="MySql.Data.MySqlClient" /> 

我的上下文

我的连接字符串3210

所以,当我按F5时,我得到了这个:“输入字符串格式不正确”。

我很想念?

UPDATE

堆栈跟踪:

[出现FormatException:输入字符串的不正确的格式]
System.Number.StringToNumber(字符串str,的NumberStyles选项, NumberBuffer & NumberFormatInfo info,Boolean parseDecimal) +9591147 System.Number.ParseUInt32(String value,NumberStyles options,NumberFormatInfo numfmt)+119
System.String.System.IConvertible.ToUInt32(的IFormatProvider提供商) 46 System.Convert.ChangeType(对象值,类型conversionType,的IFormatProvider提供商)9509065
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.ChangeType(对象 值, T型)240
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.SetValue(字符串 关键字,对象的值)399
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item(字符串 关键字,对象的值)54
系统.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)+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.InternalSet 1.Initialize() +62
System.Data.Entity.Internal.Linq.InternalSet
1.GetEnumerator() 15
System.Data.Entity.Infrastructure.DbQuery 1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40 System.Collections.Generic.List 1..ctor(IEnumerable的源)58 SimpleMysqlCrud.Controllers.PessoaController.Index()在 F:\用户\ pablo.rocha.fti \文件\ Visual Studio 2010 \ Projects \ SimpleMysqlCrud \ SimpleMysqlCrud \ Controllers \ PessoaController。CS:16 lambda_method(封闭,ControllerBase,对象[])62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,对象[]参数)17
System.Web.Mvc.ReflectedActionDescriptor.Execute (ControllerContext controllerContext,IDictionary parameters)+27
System.Web.Mvc。 <> C_ DisplayClass15.b _12() 55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 滤波器,ActionExecutingContext preContext,函数功能1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
1个滤波器,ActionDescriptor actionDescriptor, IDictionary的2 parameters) +191
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
1.b__7(IAsyncResult的 ) 12 System.Web.Mvc.Async.WrappedAsyncResult`1.End()62 System.Web.Mvc。<> c^ _DisplayClasse.b_ d()50
System.Web.Mvc.SecurityUtil.b
_0(动作f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(动作动作) +22 Sys tem.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一步,布尔& completedSynchronously)184

+0

哪条线是个例外呢? –

+1

您忘记了在** port = 8080之后添加分号**否? –

+0

@AndreyVoloshin,请举个例子。 – Pablo

回答

1

收集所有意见到答案。

port = 3306后加分号;并检查你的mysql侦听端口(可能是3306,默认是mysql?)。生成的连接字符串:

<add name="EntityContext" connectionString="Server=127.0.0.1; port=3306; Database=simple_crud; Uid=root; Pwd=;" providerName="MySql.Data.MySqlClient" /> 
+0

就像一个魅力!谢谢!!! – Pablo

+0

不客气) –

+0

端口为默认。这是没有必要的 –

0

MySql默认端口为3306;因此,没有必要在这里提到这一点,

如:

<add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="SERVER=localhost;DATABASE=testdb;UID=test;PASSWORD=test123;" />