我遵循此教程: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.
如何使我的代码根据模型创建数据库?
注释掉不受支持的DbInitializers是第一步。我已经对您的代码进行了测试,并且可以正常工作。您可以从这里获得工作示例。我已经手动创建了记事表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句