0

I have created a class which models my existing table in MySql database. Below is the class:

  public class class1
{
    [Key]
    public int id { get; set; }

    public int LastName { get; set; }
    public int FirstName { get; set; }

}

I have also created a this class.

public class Dbo : DbContext
{
    public DbSet<class1> Classs { get; set; }
}

I have created my controller. Now when I run the application, I get this error:

 MySql.Data.MySqlClient.MySqlException was unhandled by user code
  Message=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 '`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    `LastName` int NOT NULL, 
    `Fi' at line 2
  Source=MySql.Data
  ErrorCode=-2147467259
  Number=1064

MySql table has this structure:

    `id` int(11) NOT NULL,
  `LastName` int(11) NOT NULL,
  `FirstName` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT
2
  • Code First? This doesn't look like "Code First" to me. The tables already exist, right? Commented Nov 14, 2012 at 11:59
  • @spender Thats Code first with existing database. Check codeproject.com/Articles/327945/…. The tables there already exist. Commented Nov 14, 2012 at 12:07

1 Answer 1

1

According to error message, EF trying to create Db for you.

If you add null initializer it should work

    public class Dbo : DbContext
    {
        public DbSet<class1> Classs { get; set; }
        public Dbo()
        {
            Database.SetInitializer<Dbo>(null);
        }
    }

Here is another codeproject article and it worked for me: Using Entity Framework 4.1 Code First with an existing database

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. You are right. When I checked the StackTrace I noticed that.Now am moving forward to create association though my underlying data has independent tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.