BrunoRamalho
BrunoRamalho

Reputation: 1786

ASP.NET MVC EF and MySQL

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.

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:

public class Note
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public int Done { get; set; }
    }

and here the the DataContext.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:

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);:

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?

Upvotes: 2

Views: 695

Answers (1)

Ognyan Dimitrov
Ognyan Dimitrov

Reputation: 6251

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.

Upvotes: 1

Related Questions