简体   繁体   English

实体框架4.1无法创建MySQL数据库

[英]Entity Framework 4.1 cannot create MySQL database

I have a WPF application in which I'm trying to use Entity Framework 4.1 Code First for Data Access Logic. 我有一个WPF应用程序,在其中尝试将Entity Framework 4.1 Code First用于数据访问逻辑。 I'm using MySQL as DBMS. 我正在使用MySQL作为DBMS。 For simplicity I have created just one data class User: 为简单起见,我只创建了一个数据类User:

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [MaxLength(50)]
    public string Name { get; set; }

    [Required]
    [MaxLength(50)]
    public string Surname { get; set; }

    [Required]
    [MaxLength(50)]
    public string Username { get; set; }

    [Required]
    [MaxLength(255)]
    public string Password { get; set; }
}

And I have an ApplicationContext class extending the DbContext like: 我有一个ApplicationContext类,它扩展了DbContext:

public class ApplicationContext : DbContext
{
    public ApplicationContext()
        : base()
    {
    }

    public DbSet<User> Users;
}

I add a connection string with the name ApplicationContext to the app.config file. 我将名称为ApplicationContext的连接字符串添加到app.config文件。

<configuration>
    <connectionStrings>
        <clear/>
        <add name="ApplicationContext" 
 connectionString="server=localhost;User Id=myuser;Password=mypassword;Persist Security Info=True;database=mydatabase" 
 providerName="MySql.Data.MySqlClient"/>
    </connectionStrings>
    <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.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </DbProviderFactories>
    </system.data>
</configuration>

And when I try to initialize the database with the following code inside the App.xaml.cs file OnStartup method, I get ProviderIncompatibleException saying The provider did not return a ProviderManifestToken string. 当我尝试使用App.xaml.cs文件的OnStartup方法中的以下代码初始化数据库时,我收到ProviderIncompatibleExceptionThe provider did not return a ProviderManifestToken string. And the inner exception says Unknown database . 内部异常表示Unknown database

Database.SetInitializer<ApplicationContext>(
    new DropCreateDatabaseIfModelChanges<ApplicationContext>());

this.AppDatabaseContext = new ApplicationContext();
this.AppDatabaseContext.Database.Initialize(true);

I think it tries to connect to the database with the database name I have provided in the connection string and fails since it does not exist. 我认为它尝试使用我在连接字符串中提供的数据库名称连接到数据库,但由于它不存在而失败。 However, I expect that it should create one if there isn't already. 但是,我希望它应该创建一个(如果尚未创建)。

I also tried to create the database by hand from MySQL and re-run the application, this time I get a different type of exception: NotSupportedException saying Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions 我还尝试通过MySQL手动创建数据库并重新运行该应用程序,这一次,我得到了另一种异常: NotSupportedException Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions

I have searched the internet and tried many things to overcome this problem but couldn't find a solution. 我已经搜索了互联网,并尝试了许多方法来克服此问题,但是找不到解决方案。

Any help will be greatly appreciated. 任何帮助将不胜感激。

It could be a problem with the version of the ado.net provider for MySQL that you are using. 您使用的MySQL的ado.net提供程序版本可能有问题。

The latest version of dotconnect for MySQL supports code-first for EF 4.1, see http://www.devart.com/news/2011/dotconnects630.html 适用于MySQL的最新版本的dotconnect支持EF 4.1的代码优先,请参见http://www.devart.com/news/2011/dotconnects630.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM