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

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

问题描述

MVC4 + Entity Framework 4.4 + MySql + POCO / Code First



我正在设置上述配置..这里是我的类:

 命名空间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>产品{get;组; }

public DbSet< ProductImage> ProductImages {get;组; }


}
}

命名空间BTD.Data
{
[Table(Product)]
public class Product
{
[Key]
public long ProductId {get;组; }

[DisplayName(Manufacturer)]
public int? ManufacturerId {get;组;

[必需]
[StringLength(150)]
public string Name {get;组;

[必需]
[DataType(DataType.MultilineText)]
public string说明{get;组;

[必需]
[StringLength(120)]
public string URL {get;组;

[必需]
[StringLength(75)]
[DisplayName(Meta Title)]
public string MetaTitle {get;组; }

[DataType(DataType.MultilineText)]
[DisplayName(Meta Description)]
public string MetaDescription {get;组;

[必需]
[StringLength(25)]
public string Status {get;组; }

[DisplayName(Create Date / Time)]
public DateTime CreateDateTime {get;组; }

[DisplayName(编辑日期/时间)]
public DateTime EditDateTime {get;组; }
}

[Table(ProductImage)]
public class ProductImage
{
[Key]
public long ProductImageId {get ;组; }

public long ProductId {get;组; }

public long? ProductVariantId {get;组; }

[必需]
public byte [] Image {get;组; }

public bool PrimaryImage {get;组; }

public DateTime CreateDateTime {get;组; }

public DateTime EditDateTime {get;组; }

}
}

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

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




  1. 数据库AND表已存在...

  2. 我还是很新的mvc,但是使用本教程

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


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


我完全失去了 - 几乎每个不同的变化的错误消息,我可以想到,但无济于事。



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


  1. MySql正常工作

  2. 我使用MySql Connector 6.5.4版本,并创建了其他ASP.net Web表单+具有零问题的实体框架应用程序

我也尝试过在我的web.config中包含/删除这个:

 < system.data> 
< DbProviderFactories>
< remove invariant =MySql.Data.MySqlClient/>
< add name =MySQL数据提供者invariant =MySql.Data.MySqlClientdescription =。用于MySQL的Net Framework数据提供者type =MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data,Version = 6.5.4.0,Culture = neutral,PublicKeyToken = c5687fc88969c44d/>
< / DbProviderFactories>



我一直在工作在这个bug的几天 - 我现在要点,我愿意支付一个人来解决它..没有笑话...我真的很喜欢使用MVC 4和剃刀 - 我很高兴开始在这个,但现在我很气馁 - 我非常感谢任何帮助/指导这个!



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



另一个注意



我正在使用默认的可视化工作室模板来创建您的MVC项目与帐户页面和其他东西。我已经删除了对添加的文件的所有引用,因为他们试图使用不存在的DefaultConnection - 所以我认为这些文件可能是导致错误的原因 - 删除后仍然没有运气 -



我只想让大家知道我正在使用可视化工作室MVC项目模板,该模板预先创建一堆文件。我将尝试从没有这些文件的一个空白的MVC项目中重新创建这一切 - 我会更新这一次,一旦我测试了



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



另一个经历相同问题的人



< >这里是另一个模拟我的一个stackoverflow问题 - 但是我尝试他的解决方案无效 - 也许有同样问题的人可以从链接中获益



更新



我最近刚刚尝试将其放入MS Sql Server中,视图脚手架无误地添加了,所以我不知道是否我的MySql数据库或连接字符串或什么...驱动我坚果..



其他参考



它的应用程序耳朵别人正在遇到与我同样的问题 - 唯一的区别是他们正在使用sql server - 我尝试调整所有我的代码遵循这个stackoverflow问题/答案这里但仍然无效



可能的FIX?



所以这很奇怪...在挂接到MS Sql Server并添加控制器之后,然后将连接字符串还原到MySql它实际上是工作与MySql ...什么样的!



所以看来,当你尝试添加你的控制器和视图脚手架(是正确的短语?)被添加与mysql连接字符串失败...然而,如果你将它连接到一个sql server db,生成脚手架/控制器,然后恢复到mysql的连接字符串它的工作....?!?!

解决方案

似乎MVC4控制台脚手架没有正确识别MySql Connection String。为控制器生成EF CRUD代码时,请更改连接字符串:

 < connectionStrings> 
< add name =BTDContextconnectionString =Data Source = host_name; Database = database_name; uid = user_id; pwd = password; providerName =System.Data.SqlClient/>
< / connectionStrings>

运行应用程序时将其更改为标准:

 < connectionStrings> 
< add name =BTDContextconnectionString =Data Source = host_name; Database = database_name; uid = user_id; pwd = password; providerName =MySql.Data.MySqlClient/>
< / connectionStrings>

请注意更改,提供商名称。


MVC4 + Entity Framework 4.4 + MySql + POCO/Code First

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

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

  1. The database AND tables already exist...
  2. I'm still pretty new with mvc but was using this tutorial

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:

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.

Here are the things i can verify...

  1. MySql is working properly
  2. I'm using MySql Connector version 6.5.4 and have created other ASP.net web forms + entity framework applications with ZERO problems

I have also tried including/removing this in my 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!

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

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 -

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

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

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

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

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

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

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

解决方案

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天全站免登陆