简体   繁体   English

使用asp.net MVC 4 / Entity Framework时无法创建控制器。

[英]Unable to create a controller when using asp.net MVC 4/Entity Framework.

I am new to asp.net, and I am now facing a problem. 我是asp.net的新手,现在遇到了问题。

I have created a project using asp.net mvc4, and I have added an entity data model and successfully connected to MySQL. 我已经使用asp.net mvc4创建了一个项目,并且添加了实体数据模型并成功连接到MySQL。 but when I tried to create a controller, I got the error message stated as below: 但是,当我尝试创建控制器时,出现如下错误消息:

"unable to retrieve metadata for "client_test.client", Unable to find the requested .NET Framework data provider. it may not be installed." “无法检索“ client_test.client”的元数据,找不到所需的.NET Framework数据提供程序。可能未安装。

from the "scaffolding options", I choose the template: 从“脚手架选项”中,选择模板:

"MVC controller with read/write actions and views using Entity Framework" “使用实体框架具有读/写操作和视图的MVC控制器”

and from the machine.config file, I could see MySQL data provider is there. 从machine.config文件中,我可以看到MySQL数据提供程序在那里。

<DbProviderFactories>
  <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>

and the connectionString in the web.config is like this: 和web.config中的connectionString像这样:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-client_test-20130916144039;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-client_test-20130916144039.mdf" providerName="System.Data.SqlClient" />
<add name="Client_infoEntities" connectionString="metadata=res://*/clientDatabase.csdl|res://*/clientDatabase.ssdl|res://*/clientDatabase.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=127.0.0.1;user id=root;password=itplustest;persist security info=True;database=Client_info&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

and also I have added the code below to the web. 并且我已经将下面的代码添加到了网络上。 config: 配置:

<system.data>
<DbProviderFactories>
  <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>

My connector/net version is 6.7.4.0 and i have MySQL for Visual Studio 1.0.2, I hope you guys could help me to solve my problem. 我的连接器/网络版本是6.7.4.0,并且我拥有适用于Visual Studio 1.0.2的MySQL,我希望你们能帮助我解决我的问题。

Thanks JY 谢谢JY

I have been "playing" (In fact, it might be more appropriate to say "fighting") with this MySql thing with CodeFirst in Visual Studio. 我一直在用Visual Studio中的CodeFirst在MySql方面“玩”(实际上,说“战斗”可能更合适)。 In fact it does work, but by difference with different readings found in googling around, I am still unable to have code first create its tables. 实际上它确实可以工作,但是与谷歌搜索中发现的不同读物不同,我仍然无法让代码首先创建其表。

Anyway, I discovered you have to be very carefull about which version of Entity Framework you are using related to which version of MySql Connector. 无论如何,我发现您必须非常小心所使用的Entity Framework版本与哪个版本的MySql Connector有关。

I made a very simple console application, which is working fine after I manually created the table. 我制作了一个非常简单的控制台应用程序,在手动创建表后,该应用程序运行良好。

I am using EntityFramework 5.0 (current version) with the 6.7.4 (beta) version of the connector (Nuget Console PM> Install-Package MySql.Data.Entity -Pre) as this version works with EF 5, while the current version of the connector works with EF 4.2. 我正在使用EntityFramework 5.0(当前版本)和连接器的6.7.4(测试版)版本(Nuget Console PM> Install-Package MySql.Data.Entity -Pre),因为该版本可用于EF 5,而当前版本为EF 5。该连接器可用于EF 4.2。

Here is my App config. 这是我的应用程序配置。 Notice there is no (as in NO) reference to Entity Framework anywhere in this config file. 请注意,在此配置文件中的任何地方都没有(如在NO中)对Entity Framework的引用。

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description="Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MyContext" connectionString="server=localhost;User Id=bernard;Password=some pwd; Persist Security Info=True;database=test" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>

Hope this helps a bit. 希望这个对你有帮助。 I am going to continue looking to see if Code First really works or not with MySql, as with MS SQL Tables and columns are created directly by code first if they don't exists. 我将继续寻找Code First是否真的适用于MySql,就像MS SQL一样。如果表和列不存在,则直接由代码直接创建。

EDIT A FEW HOURS LATER * * I finally was able to have code first create the table and columns, yepee! 编辑几小时后* *我终于能够让代码首先创建表和列,是的! :-) :-)

I explicitly called Database.SetInitializer, which I thought was not needed. 我明确地调用了Database.SetInitializer,我认为不需要它。 Anyway, "REAL" Code First with MySql is now working just fine. 无论如何,MySql的“ REAL”代码优先现在可以正常工作。

I wrote a blog post about this, and you can download the project from this post: http://www.bgsoftfactory.net/entity-framework-with-code-first-and-mysql-5-6-14-current-version/ 我写了一篇关于此的博客文章,您可以从此文章下载该项目: http : //www.bgsoftfactory.net/entity-framework-with-code-first-and-mysql-5-6-14-current-版/

Have you been able to create a data connection from Visual Studio/Server explorer ? 您是否能够通过Visual Studio / Server Explorer创建数据连接? I also see that your default connection point to LocalDB (MSSQL), which might be what you want, but I would keep it as simple as possible to start. 我还看到您的默认连接点是LocalDB(MSSQL),这可能就是您想要的,但是我会尽可能地简化启动过程。

Have a look there, it may help you: http://blog.jongallant.com/2013/04/mysql-aspnet-mvc-entity-framework.html#.UjmwVMbKEqI 在那里看看,可能会对您有所帮助: http : //blog.jongallant.com/2013/04/mysql-aspnet-mvc-entity-framework.html#.UjmwVMbKEqI

Regards, Bernard 此致,伯纳德

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 问题,VS 19 ASP.NET Core 3.1 当我开始创建“MVC Controller with view, using Entity Framework” - Issue, VS 19 ASP.NET Core 3.1 when I start to create “MVC Controller with view, using Entity Framework” asp.net mvc 5.1 mysql实体框架控制器生成 - asp.net mvc 5.1 mysql Entity Framework Controller Generation Controller中的ASP.NET MVC实体框架DB访问 - ASP.NET MVC Entity Framework DB access in Controller 使用Entity Framework MVC ASP.NET创建具有自定义类型的控制器时出错 - Error Creating Controller With Custom Type using Entity Framework MVC ASP.NET ASP.NET MVC / Entity Framework - Controller 生成以某种方式使用旧属性 - ASP.NET MVC / Entity Framework - Controller generation is somehow using old properties 使用MVC控制器实体框架创建控制器时,显示“不受支持的上下文类型”。 MVC4 - “Unsupported context type” while creating a controller using MVC controller Entity Framework. MVC4 如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库? - How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework? 用于数据库词典的ASP.NET通用控制器(使用实体框架) - ASP.NET universal controller for database dictionaries (using Entity Framework) 如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表 - How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc 无法在 ASP.NET Core 6 MVC 框架中创建区域 - Unable to create Area in ASP.NET Core 6 MVC framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM