locked
EntityFramework(Code First) and MySql RRS feed

  • Question

  • I'm experimenting with EntityFramework (version 4.3.1) and CodeFirst Dev in my project. After I've defined my models and extended the DBContext as the sample on msdn, I found that the code run correctly on SQLServer, but not on MySql (version 5.1.61,connector/net version 6.4.4). The exception was like this:


    Exception:  MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
            'NOT NULL,
            `ProductVersion` mediumtext NOT NULL);
    ALTER TABLE `HistoryRow` ' at line 6

    Then I tried capturing the SQL scripts which the EntityFramework auto-generated, some statements in the scripts brought to my attention:

    CREATE TABLE `__MigrationHistory`(
     `MigrationId` mediumtext NOT NULL,
     `CreatedOn` datetime NOT NULL,
     `Model` varbinary NOT NULL,
     `ProductVersion` mediumtext NOT NULL);

    I copied it in the environment of MySql command line client, and got the same error message. By checking this statement block, I found the keyword "varbinary" was not supported in MySql. It could run correctly if I changed "varbinary" to "varbinary(1)". Even so, I can't change the auto-generation of EntityFramework, of course.
    So, is there anybody to help me out? Is there something I'm doing wrong here?



    Wednesday, March 7, 2012 7:11 AM

Answers

  • I'm glad to tell everyone,in the end,i changed referenced assembly of EntityFramework to the one of the version 4.1.10715  for avoiding those features of the code first migrations.It works fine now.

    However,i still expect the improvment of the newest EntityFramework.

    • Marked as answer by Allen_MSDN Monday, March 12, 2012 2:05 AM
    Friday, March 9, 2012 8:26 AM

All replies

  • Hi Bin.Xiang,

    Welcome to MSDN Forum.

    Based on the issue, could you please post the code here? I think I can help you more effectively with the code. By the way, if the version of MySQL Provider is not the latest one, I suggest you to upgrade it to the latest version.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 8, 2012 7:19 AM
  •  Allen_Li1988,Thanks for your reply.

    Here are my codes(it's simple, right?):

    public class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } } public class Blog : DbContext { public IDbSet<Post> Posts { get; set; } }

    static void Main(string[] args)
    {
        using (var blog = new Blog())
        {
              var post = new Post()
              {
                  Title = "Test Title",
                  Content = "This is my content for test"
              };
              blog.Posts.Add(post);
              blog.SaveChanges();
         }
    }

    In app.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="Blog" connectionString="server=localhost;User Id=root;Pwd=password;Persist Security Info=True;database=Blog" providerName="MySql.Data.MySqlClient" />
      </connectionStrings>
    </configuration>

    this time,i changed my environment :

    EntityFramework: 4.3.1

    .Net Framework: 4.0

    MySql: 5.5

    Connector/Net: 6.5.4

    but,the exception remains there like before ,for just first running.

    Thursday, March 8, 2012 10:31 AM
  • Hi Bin.Xiang,

    The demo is simple, I tested it in my machine(Entity Framework: 4.3.1  .NET Framework: 4.0  Server:  SQL Server2008R2), it works well. I suggest you to post the question in MySQL Forum, some specialists about MySQL may help you to find the root cause.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 9, 2012 2:42 AM
  • I'm glad to tell everyone,in the end,i changed referenced assembly of EntityFramework to the one of the version 4.1.10715  for avoiding those features of the code first migrations.It works fine now.

    However,i still expect the improvment of the newest EntityFramework.

    • Marked as answer by Allen_MSDN Monday, March 12, 2012 2:05 AM
    Friday, March 9, 2012 8:26 AM
  • Hi Bin.Xiang,

    I'm glad to hear that you have solved the issue. Please be patient with Entity Framework, it is still on he way, I think it will be a perfect product in the future.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 12, 2012 2:06 AM
  • I wrote an article about EF5 Code First + MySQL

    By using the latest MySQL .NET connector (6.7.4) which supports EF 5 now, it seems that there are less steps involved.

    http://dleusden.blogspot.de/2013/09/mysql-55-and-ef-5-code-first.html

    I hope it helps!
    Sunday, September 1, 2013 2:51 PM