我遵循本教程:http://www.asp.net/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider 一切都按预期工作。
但是现在我想将我的模型添加到应用程序中,但是我在连接时遇到了创建数据库的问题
这里有一个模型Note.cs:
public class Note
{
public int Id { get; set; }
public string Text { get; set; }
public int Done { get; set; }
}
这里是DataContext.cs
public class DataContext : DbContext
{
public DataContext()
: base("DefaultConnection")
{
}
public DbSet<Note> Note { get; set; }
static DataContext()
{
Database.SetInitializer<DataContext>(
new DropCreateDatabaseAlways<DataContext>());
}
}
和HomeController.cs:
private DataContext db = new DataContext();
public ActionResult Index()
{
var note = new Note { Id = 1, Text = "this is the text", Done = 0 };
db.Note.Add(note);
db.SaveChanges();
ViewBag.Notes = db.Note.ToList();
return View();
}
当我进行登录时,一切都很完美,它为用户创建了数据库。
但是当我转到这个控制器时,我在db.Note.Add(note);
上发现了这个错误:
An exception of type 'System.NotSupportedException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
如何让我的代码根据模型创建数据库?