I just wanted to try Entity Framework to use it instead of NHibernate. I'm using a MySql database. So I did few tests with the code first system. Everything is ok with MsSql the database and the schema is auto generated. But then I tried it with MySql and the schema generation doesn't work.
public class Context : DbContext
{
public Context()
{
}
public Context(string str)
: base (str)
{
}
public DbSet<User> Users
{
get;
set;
}
}
public class User
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
var sqlBuilder =
new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = "test",
Password = "",
UserID = "root"
};
var entityBuilder =
new EntityConnectionStringBuilder
{
Provider = "MySql.Data.MySqlClient",
ProviderConnectionString = sqlBuilder.ConnectionString,
Metadata = @"res://*"
};
var str = entityBuilder.ConnectionString;
using (var ctx = new Context(str))
{
ctx.Users.Add(new User() // exception here
{
Name = "test"
});
ctx.SaveChanges();
}
using (var ctx = new Context(str))
{
foreach (var user in ctx.Users)
{
Console.WriteLine("id:{0} name:{1}", user.Id, user.Name);
}
}
}
}
And I got this exception : "The entity type User is not part of the model for the current context."
So here is my question, can I generate the database automatically if I use MySql ?