MySql和实体框架代码优先

时间:2013-01-16 15:01:27

标签: c# mysql frameworks entity

我遇到MySql和Entity Framework Code First的问题。

web.config.xml上的My Connection String

<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&安培; number,NumberFormatInfo info,Boolean parseDecimal)   +9591147 System.Number.ParseUInt32(字符串值,NumberStyles选项,NumberFormatInfo numfmt)+119
  System.String.System.IConvertible.ToUInt32(IFormatProvider provider)   +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
  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(Type   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的1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable 1   源码)+58 SimpleMysqlCrud.Controllers.PessoaController.Index()in   f:\ users \ pablo.rocha.fti \ documents \ visual studio   2010 \项目\ SimpleMysqlCrud \ SimpleMysqlCrud \ \控制器PessoaController.cs:16   lambda_method(Closure,ControllerBase,Object [])+62
  System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase   controller,Object []参数)+17
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2   参数)+27
  System.Web.Mvc&LT;&GT; C_ DisplayClass15.b _12()   +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter)   filter,ActionExecutingContext preContext,Func 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。&lt;&gt; c _DisplayClasse.b_ d()+50 <登记/>   System.Web.Mvc.SecurityUtil.b _0(动作f)+7   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)   +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&amp; completedSynchronously)+184

2 个答案:

答案 0 :(得分:2)

将所有评论收集到答案中。

在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" />

答案 1 :(得分:-1)

MySql默认端口是3306;所以没必要在这提及,

例如:

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