无法检索元数据

时间:2012-09-22 18:27:45

标签: asp.net mysql asp.net-mvc entity-framework ef-code-first

MVC4 +实体框架4.4 + MySql + POCO /代码优先

我正在设置上面的配置..这是我的课程:

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还很新,但使用的是this tutorial
  3. 应用程序构建正常...但是当我尝试使用Product(BTD.Data)作为我的模型类和BTDContext(BTD.DataContext)作为我的数据上下文类添加控制器时,我收到以下错误:

      

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

    我完全失去了 - 我已经在谷歌上搜索了几乎所有我可以想到的错误信息的不同变体,但无济于事。

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

    1. MySql正常运行
    2. 我正在使用MySql Connector版本6.5.4并创建了其他具有ZERO问题的ASP.net Web表单+实体框架应用程序
    3. 我还尝试在我的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>
      

      我已经在这个问题上工作了好几天 - 我现在已经到了这一点,我愿意付钱给别人来解决它......不是开玩笑......我真的很喜欢使用MVC 4和剃刀 - 我很高兴开始这个,但现在我很沮丧 - 我非常感谢任何帮助/指导!

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

      另一个注意事项

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

      我只想让每个人都知道我正在使用Visual Studio MVC项目模板,它预先创建了一堆文件。我将尝试从没有这些文件的空白MVC项目中重新创建这一切 - 我将在测试后更新此内容

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

      经历同样问题的另一个人

      Right here is another stackoverflow question that mimics mine - 但我尝试了他的解决方案无济于事 - 也许有其他人遇到同样的问题可以从链接中受益

      更新

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

      其他参考资料

      似乎其他人遇到了同样的问题 - 唯一的区别是他们使用的是sql server - 我尝试调整所有代码以遵循this stackoverflow question/answer here上的建议,但仍无济于事

      可能修复???

      所以这很奇怪......在将它连接到MS Sql Server并添加控制器之后,然后将连接字符串恢复为MySql它实际上正在使用MySql ......真是太棒了!

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

6 个答案:

答案 0 :(得分:24)

似乎MVC4 Controller脚手架无法正确识别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>

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

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

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

答案 1 :(得分:10)

imesh暗示几乎解决了我的问题,但另外我临时评论了一行

[DbConfigurationType(typeof(MySqlEFConfiguration))]

在DBContext类中。当然,在创建控制器后,该行应该取消注释,并在配置文件中将System.Data.SqlClient更改回MySql.Data.MySqlClient。

答案 2 :(得分:9)

使用VS 2013,MySQL Connector / NET 6.9.6,Entity Framework 6,Web API 2.2和MySQL服务器5.7我必须结合答案以防止关于&#34;无法检索元数据的错误&#34;。< / p>

要成功将控制器添加到使用MySQL连接的.NET项目,请执行以下操作:

  1. 暂时添加System.Data.SqlClient作为providerName的连接字符串,并为MySQL注释一个。连接字符串是否有效并不重要。
  2. 确保MySqlEFConfiguration未以任何方式启用。
  3. 重建。
  4. 关于第二点,MySQL documentation on using Connector/NET with EF6说明了启用MySqlEFConfiguration的三种可能方式。确保在使用VS模板添加控制器时未启用这些功能。

      
        
    1. 在上下文类中添加 DbConfigurationTypeAttribute
    2.         

      [DbConfigurationType(typeof(MySqlEFConfiguration))]

           
          
      1. 调用 DbConfiguration.SetConfiguration(new MySqlEFConfiguration())   在应用程序启动

      2.   
      3. 在配置文件中设置 DbConfiguration 类型:

      4.         

        <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

答案 3 :(得分:1)

我还测试了这个bug并看到了另一个问题。

以下代码在我的Web.config中(没有,它不起作用):

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

将其更改为:

<entityFramework>

为我工作...添加脚手架,然后将其更改回来

答案 4 :(得分:0)

使用EF 4.3.1和MySql Connecter / Net 6.6.4.0我遇到了同样的问题

这对我有用,无需连接到Context类中的不同数据库或额外代码。

当您要构建脚手架时,在web.config文件中的entityFramework标记之间添加此内容:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
  </parameters>
</defaultConnectionFactory>

然后在您想要运行迁移时注释上面的代码,反之亦然。

所以你的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>

这是非常荒谬的.NET开发人员必须跳过一些艰难的环节才能让他们的代码正常工作。

工作示例

https://github.com/dublinan/mvc-mysql-ef-example

指向我的github项目的链接

答案 5 :(得分:0)

请尝试使用

  

System.ComponentModel.DataAnnotations

命名空间以及EDM类成员上的[Key]属性。

它对我有用。