I follow this tutorial: http://www.asp.net/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider And everything works as expected.
我遵循本教程:http://www.asp.net/identity/overview/gettingstarted/aspnet -mysql-storage- mysql-provider,所有的东西都按预期工作。
But now I want to add my model to the application, but I'm having problems to connect to create the database
但是现在我想将我的模型添加到应用程序中,但是我在创建数据库时遇到了连接问题
Here one model Note.cs:
这一个模型Note.cs:
public class Note
{
public int Id { get; set; }
public string Text { get; set; }
public int Done { get; set; }
}
and here the the DataContext.cs
这里是datacontex .cs
public class DataContext : DbContext
{
public DataContext()
: base("DefaultConnection")
{
}
public DbSet<Note> Note { get; set; }
static DataContext()
{
Database.SetInitializer<DataContext>(
new DropCreateDatabaseAlways<DataContext>());
}
}
and the the HomeController.cs:
和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();
}
When I make the login everything is perfect, It creates the database for users. But when I go to this controller I got this error on db.Note.Add(note);
:
当我完成登录时,它会为用户创建数据库。但是当我访问这个控制器时,我在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.
How can I make my code create the database according to the model?
如何让代码根据模型创建数据库?
1 个解决方案
#1
1
Commenting out the unsupported DbInitializers is the first step. I have done a test with your code and it works. You can get the working example from here. I have created the Note table by hand.
第一步是注释不支持的DbInitializers。我用你的代码做了一个测试,它是有效的。您可以从这里得到工作示例。我已经手工创建了Note表。
#1
1
Commenting out the unsupported DbInitializers is the first step. I have done a test with your code and it works. You can get the working example from here. I have created the Note table by hand.
第一步是注释不支持的DbInitializers。我用你的代码做了一个测试,它是有效的。您可以从这里得到工作示例。我已经手工创建了Note表。