MySQL 无法检索元数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12546545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to Retrieve Metadata
提问by 99823
MVC4 + Entity Framework 4.4 + MySql + POCO/Code First
MVC4 + Entity Framework 4.4 + MySql + POCO/代码优先
I'm setting up the above configuration .. here are my classes:
我正在设置上述配置..这是我的课程:
namespace BTD.DataContext
{
public class BTDContext : DbContext
{
public BTDContext()
: base("name=BTDContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Conventions.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
}
public DbSet<Product> Products { get; set; }
public DbSet<ProductImage> ProductImages { get; set; }
}
}
namespace BTD.Data
{
[Table("Product")]
public class Product
{
[Key]
public long ProductId { get; set; }
[DisplayName("Manufacturer")]
public int? ManufacturerId { get; set; }
[Required]
[StringLength(150)]
public string Name { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required]
[StringLength(120)]
public string URL { get; set; }
[Required]
[StringLength(75)]
[DisplayName("Meta Title")]
public string MetaTitle { get; set; }
[DataType(DataType.MultilineText)]
[DisplayName("Meta Description")]
public string MetaDescription { get; set; }
[Required]
[StringLength(25)]
public string Status { get; set; }
[DisplayName("Create Date/Time")]
public DateTime CreateDateTime { get; set; }
[DisplayName("Edit Date/Time")]
public DateTime EditDateTime { get; set; }
}
[Table("ProductImage")]
public class ProductImage
{
[Key]
public long ProductImageId { get; set; }
public long ProductId { get; set; }
public long? ProductVariantId { get; set; }
[Required]
public byte[] Image { get; set; }
public bool PrimaryImage { get; set; }
public DateTime CreateDateTime { get; set; }
public DateTime EditDateTime { get; set; }
}
}
Here is my web.config setup...
这是我的 web.config 设置...
<connectionStrings>
<add name="BTDContext" connectionString="Server=localhost;Port=3306;Database=btd;User Id=root;Password=mypassword;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
- The database AND tables already exist...
- I'm still pretty new with mvc but was using this tutorial
- 数据库 AND 表已经存在...
- 我对 mvc 还是很陌生,但正在使用本教程
The application builds fine... however when I try to add a controller using Product (BTD.Data) as my model class and BTDContext (BTD.DataContext) as my data context class I receive the following error:
该应用程序构建良好......但是,当我尝试使用 Product (BTD.Data) 作为我的模型类和 BTDContext (BTD.DataContext) 作为我的数据上下文类添加控制器时,我收到以下错误:
Unable to retrieve metadata for BTD.Data.Product using the same DbCompiledModel to create context against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used.
不支持使用相同的 DbCompiledModel 为不同类型的数据库服务器创建上下文来检索 BTD.Data.Product 的元数据。相反,为正在使用的每种类型的服务器创建一个单独的 DbCompiledModel。
I am at a complete loss - I've scoured google with almost every different variation of that error message above I can think of but to no avail.
我完全不知所措 - 我已经在谷歌上搜索了几乎所有我能想到的错误消息的不同变体,但无济于事。
Here are the things i can verify...
以下是我可以验证的内容...
- MySql is working properly
- I'm using MySql Connector version 6.5.4 and have created other ASP.net web forms + entity framework applications with ZERO problems
- MySql 工作正常
- 我正在使用 MySql 连接器 6.5.4 版,并创建了其他零问题的 ASP.net Web 表单 + 实体框架应用程序
I have also tried including/removing this in my web.config:
我也尝试在我的 web.config 中包含/删除它:
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<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.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
I've literally been working on this bug for days - I'm to the point now that I would be willing to pay someone to solve it.. no joke... I'd really love to use MVC 4 and Razor - I was so excited to get started on this, but now i'm pretty discouraged - I truly appreciate any help/guidance on this!
我真的已经研究这个 bug 好几天了 - 现在我愿意花钱请人来解决它......不是开玩笑......我真的很喜欢使用 MVC 4 和 Razor - 我开始做这件事很兴奋,但现在我很气馁 - 我真的很感谢任何帮助/指导!
Also note - i'm using Entityframework from Nuget...
另请注意 - 我正在使用 Nuget 的 Entityframework ...
Another Note
另一个注意事项
I was using the default visual studio template that creates your MVC project with the account pages and other stuff. I JUST removed all references to the added files because they were trying to use the "DefaultConnection" which didn't exist - so i thought those files may be what was causing the error - however still no luck after removing them -
我使用的是默认的 Visual Studio 模板,该模板使用帐户页面和其他内容创建您的 MVC 项目。我只是删除了对添加文件的所有引用,因为他们试图使用不存在的“DefaultConnection” - 所以我认为这些文件可能是导致错误的原因 - 但是在删除它们后仍然没有运气 -
I just wanted to let everyone know i'm using the visual studio MVC project template which pre-creates a bunch of files. I will be trying to recreate this all from a blank MVC project which doesn't have those files - i will update this once i test that
我只是想让大家知道我正在使用 Visual Studio MVC 项目模板,它预先创建了一堆文件。我将尝试从一个没有这些文件的空白 MVC 项目中重新创建这一切 - 我将在测试后更新它
UPDATE TO USING VS MVC Basic Template:Same error resulted - still no solution
更新使用 VS MVC 基本模板:导致相同的错误 - 仍然没有解决方案
ANOTHER PERSON EXPERIENCING THE SAME PROBLEM
另一个遇到同样问题的人
Right here is another stackoverflow question that mimics mine- however I tried his solution to no avail - maybe someone else who is having this same problem can benefit from the link
这里是另一个模仿我的 stackoverflow 问题- 但是我尝试了他的解决方案无济于事 - 也许其他遇到同样问题的人可以从链接中受益
UPDATE
更新
I recently just tried putting this into MS Sql Server and the view scaffolding adds fine with no error - so I'm not sure if its my MySql database or connection string or what... driving me nuts..
我最近刚刚尝试将它放入 MS Sql Server 并且视图脚手架添加得很好,没有错误 - 所以我不确定它是我的 MySql 数据库或连接字符串还是什么......让我发疯..
Other References
其他参考
It appears someone else is having the same issues I am - the only difference is they are using sql server - I tried tweaking all my code to follow the suggestions on this stackoverflow question/answer herebut still to no avail
似乎其他人遇到了与我相同的问题 - 唯一的区别是他们使用的是 sql server - 我尝试调整我的所有代码以遵循此 stackoverflow 问题/答案中的建议,但仍然无济于事
POSSIBLE FIX???
可能的修复???
So this is weird... after hooking it up to MS Sql Server and adding the controller, then reverting the connection string to MySql it is actually WORKING with MySql... what the heck!??
所以这很奇怪......在将它连接到 MS Sql Server 并添加控制器之后,然后将连接字符串恢复到 MySql 它实际上正在与 MySql 一起工作......到底是什么!??
So it seems that when you try to add your controller and the view scaffolding (is that the right phrase?) is added WITH the mysql connection string it fails...however if you hook it up to a sql server db, generate the scaffolding/controller, then revert to mysql connection string it works.... ?!?!
因此,似乎当您尝试添加控制器和视图脚手架(这是正确的短语吗?)与 mysql 连接字符串一起添加时,它会失败……但是,如果您将其连接到 sql server db,请生成脚手架/controller,然后恢复到 mysql 连接字符串,它起作用了......?!?!
回答by imesh
It seems that MVC4 Controller scaffolding is not properly recognizing MySql Connection String. Change the connection string as shown below when generating EF CRUD code for Controllers:
MVC4 控制器脚手架似乎没有正确识别 MySql 连接字符串。在为控制器生成 EF CRUD 代码时更改连接字符串,如下所示:
<connectionStrings>
<add name="BTDContext" connectionString="Data Source=host_name;Database=database_name;uid=user_id;pwd=password;" providerName="System.Data.SqlClient" />
</connectionStrings>
Change it back to standard when running the application:
运行应用程序时将其改回标准:
<connectionStrings>
<add name="BTDContext" connectionString="Data Source=host_name;Database=database_name;uid=user_id;pwd=password;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Note the change, provider name.
注意更改,提供程序名称。
回答by Yurko
The imesh suggesting almost solve my problem, but additionally I temporary commented line
imesh 提示几乎解决了我的问题,但另外我临时注释了行
[DbConfigurationType(typeof(MySqlEFConfiguration))]
which was in DBContext class. And of course after creation controller this line should be uncommented and change back System.Data.SqlClient to MySql.Data.MySqlClient in config file.
这是在 DBContext 类中。当然,在创建控制器之后,应该取消注释这一行,并将配置文件中的 System.Data.SqlClient 改回 MySql.Data.MySqlClient。
回答by AronVanAmmers
Using VS 2013, MySQL Connector/NET 6.9.6, Entity Framework 6, Web API 2.2 and MySQL server 5.7 I had to combine the answers to prevent the error about "Unable to retrieve metadata".
使用 VS 2013、MySQL 连接器/NET 6.9.6、实体框架 6、Web API 2.2 和 MySQL 服务器 5.7,我必须合并答案以防止出现“无法检索元数据”的错误。
To successfully add a controller to a .NET project that uses a MySQL connection, do the following:
要将控制器成功添加到使用 MySQL 连接的 .NET 项目,请执行以下操作:
- Temporarily add a connection string with
System.Data.SqlClient
as theproviderName
, and comment the one for MySQL. It doesn't matter whether the connection string is valid. - Ensure that
MySqlEFConfiguration
isn't enabled in any way. - Rebuild.
- 临时添加一个带有
System.Data.SqlClient
as 的连接字符串providerName
,并为 MySQL 注释该字符串。连接字符串是否有效并不重要。 - 确保
MySqlEFConfiguration
未以任何方式启用。 - 重建。
About the second point, the MySQL documentation on using Connector/NET with EF6states three possible ways to enable the MySqlEFConfiguration
. Ensure that none of these are enabled while adding controllers using the VS template.
关于第二点,关于将 Connector/NET 与 EF6 一起使用的MySQL 文档说明了三种可能的方法来启用MySqlEFConfiguration
. 确保在使用 VS 模板添加控制器时没有启用这些。
- Adding the DbConfigurationTypeAttributeon the context class:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration())at the application startup
Set the DbConfigurationtype in the configuration file:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
- 在上下文类上添加DbConfigurationTypeAttribute:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
在应用程序启动时调用DbConfiguration.SetConfiguration(new MySqlEFConfiguration())
在配置文件中设置DbConfiguration类型:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
回答by Suplanus
I tested also around this bug and saw an other problem.
我也围绕这个错误进行了测试,发现了另一个问题。
Following code is in my Web.config (without, it don't work):
以下代码在我的 Web.config 中(没有,它不起作用):
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
Changed it to:
改为:
<entityFramework>
Works for me... add the scaffolding and then change it back
对我有用...添加脚手架,然后将其改回
回答by Abhinaw
Please try using the
请尝试使用
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations
namespace along with the [Key] attribute on the EDM class members.
命名空间以及 EDM 类成员上的 [Key] 属性。
It worked for me.
它对我有用。
回答by Andre Dublin
I've been having the same problem using EF 4.3.1 and MySql Connecter/Net 6.6.4.0
我在使用 EF 4.3.1 和 MySql Connecter/Net 6.6.4.0 时遇到了同样的问题
This worked for me, no need to connect to a different db or extra code in the Context class.
这对我有用,无需连接到 Context 类中的不同数据库或额外代码。
Add this between the entityFramework tags in your web.config file when you want to build a scaffold:
当你想要构建一个脚手架时,在你的 web.config 文件中的 entityFramework 标签之间添加这个:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
Then comment the above code out when you want to run migrations and vice versa.
然后当你想运行迁移时注释掉上面的代码,反之亦然。
So you web.config will look like so:
所以你的 web.config 看起来像这样:
<entityFramework>
<contexts>
<context type="ApptManager.Models.AppointmentsManagerContext, ApptManager">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[ApptManager.Models.AppointmentsManagerContext, ApptManager], [ApptManager.Migrations.Configuration, ApptManager]], EntityFramework" />
</context>
</contexts>
<!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>-->
</entityFramework>
This is quite ridiculous how .NET developers have to jump through some arduous hoops in order to get their code working.
.NET 开发人员必须跳过一些艰巨的任务才能使他们的代码正常工作,这是非常荒谬的。
Working example
工作示例
https://github.com/dublinan/mvc-mysql-ef-example
https://github.com/dublinan/mvc-mysql-ef-example
Links to my github project
链接到我的 github 项目