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
According to error message, EF trying to create Db for you.
If you add null initializer it should work
Here is another codeproject article and it worked for me: Using Entity Framework 4.1 Code First with an existing database