无法检索元数据 [英] Unable to Retrieve Metadata

查看:31
本文介绍了无法检索元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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; }

}
}

这是我的 web.config 设置...

<connectionStrings>
<add name="BTDContext" connectionString="Server=localhost;Port=3306;Database=btd;User Id=root;Password=mypassword;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

  1. 数据库 AND 表已经存在...
  2. 我对 mvc 还是很陌生,但正在使用 本教程

应用程序构建良好...但是,当我尝试使用 Product (BTD.Data) 作为我的模型类和 BTDContext (BTD.DataContext) 作为我的数据上下文类添加控制器时,我收到以下错误:

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:

无法检索 BTD.Data.Product 使用相同的元数据DbCompiledModel 针对不同类型的数据库创建上下文不支持服务器.相反,创建一个单独的 DbCompiledModel对于正在使用的每种类型的服务器.

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.

我完全不知所措 - 我已经在谷歌上搜索了几乎所有我能想到的错误消息的不同变体,但无济于事.

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.

以下是我可以验证的内容...

  1. MySql 工作正常
  2. 我使用的是 MySql 连接器 6.5.4 版,并创建了其他零问题的 ASP.net Web 表单 + 实体框架应用程序

我也尝试在我的 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>

我确实已经研究这个 bug 好几天了 - 现在我愿意花钱请人来解决它......不是开玩笑......我真的很喜欢使用 MVC 4 和Razor - 开始这件事我很兴奋,但现在我很沮丧 - 我真的很感激任何关于这方面的帮助/指导!

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!

另请注意 - 我正在使用 Nuget 的 Entityframework...

Also note - i'm using Entityframework from Nuget...

另一个注意事项

我使用的是默认的 Visual Studio 模板,该模板使用帐户页面和其他内容创建您的 MVC 项目.我只是删除了对添加文件的所有引用,因为他们试图使用不存在的DefaultConnection" - 所以我认为这些文件可能是导致错误的原因 - 但是删除它们后仍然没有运气 -

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 项目模板,它预先创建了一堆文件.我将尝试从一个没有这些文件的空白 MVC 项目中重新创建这一切 - 我将在测试后更新它

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

更新使用 VS MVC 基本模板: 导致同样的错误 - 仍然没有解决方案

UPDATE TO USING VS MVC Basic Template: Same error resulted - still no solution

另一个人遇到了同样的问题

这里是另一个stackoverflow模仿我的问题 - 但是我尝试了他的解决方案无济于事 - 也许遇到同样问题的其他人可以从链接中受益

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

更新

我最近刚刚尝试将其放入 MS Sql Server 并且视图脚手架添加良好且没有错误 - 所以我不确定它是否是我的 MySql 数据库或连接字符串或什么......让我发疯..

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..

其他参考资料

似乎其他人遇到了与我相同的问题 - 唯一的区别是他们使用的是 sql server - 我尝试调整我的所有代码以遵循 这里是这个stackoverflow问题/答案但仍然无济于事

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 here but still to no avail

可能的修复???

所以这很奇怪……在将它连接到 MS Sql Server 并添加控制器之后,然后将连接字符串恢复到 MySql,它实际上正在与 MySql 一起工作……到底是什么!??

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!??

因此,似乎当您尝试添加控制器和视图脚手架(这是正确的短语吗?)与 mysql 连接字符串一起添加时,它会失败……但是,如果您将其连接到 sql server 数据库,生成脚手架/控制器,然后恢复到它工作的 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.... ?!?!

推荐答案

似乎 MVC4 控制器脚手架没有正确识别 MySql 连接字符串.为Controllers生成EF CRUD代码时更改连接字符串如下所示:

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:

<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.

这篇关于无法检索元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆