开发者

How to refresh my database

I was creating an mvc application and i had a model class with a dbcontext function the variables were in the same file. I added another field and changed the code in the edit create delete details and index views but i now get an error saying

"The model backing the 'GameDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and opti开发者_运维知识库onally seed it with new data."

    Imports System.Data.Entity

    Public Class Game
        Public Property ID() As Integer
        Public Property Name() As String
        Public Property Genre() As String
        Public Property Price() As Decimal
        Public Property Developer() As String
    End Class
    Public Class GameDBContext
        Inherits DbContext
        Public Property Games() As DbSet(Of Game)
    End Class


you need to add a initializer method to the Application_Start method in Global.asax file. Ex:-

 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<GameDBContext>());


Since it sounds like you're using a code first approach in developing your application, here are a couple of links that might help you:

code first development with entity framework 4

Code First/Entity Framework 4.1 Videos and Articles on MSDN

All of these can give you a better understanding of what is happening and how to work around having your database change on you.

In short, since you added another property, the entity framework table in your database is now longer in sync with your class. Since it's no longer in sync, you must manually delete this database in order to regenerate it, or you must create an initialization for the database. All of these concepts are better described in the links I have provided.

Good luck, and hope these help you some.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜