2012-07-26 46 views
0

我已经看到其他答案,但我无法使其工作。我有一个简单,干净的网站有两种型号:支持上下文的模型已更改

博客型号:

Imports System.Data.Entity 
Imports System.ComponentModel.DataAnnotations 

Public Class Blog 

    Public Property BlogId() As Integer 

    Public Property Name() As String 
    Public Property Description() As String 
    Public Property DateCreated As Date 

    Public Overridable Property Articles() As ICollection(Of Article) 

End Class 

Public Class BlogDbContext 

    Inherits DbContext 
    Public Property Blogs As DbSet(Of Blog) 

End Class 

文章型号:

Imports System.Data.Entity 
Imports System.ComponentModel.DataAnnotations 

Imports GemcoBlog 

Public Class Article 

    Public Property ArticleId() As Integer 
    Public Property BlogId() As Integer 
    Public Property Title() As String 
    Public Property Body() As String 
    Public Property DateCreated As Date 

Public Overridable Property Blog() As Blog 

End Class 

Public Class ArticleDbContext 
    Inherits DbContext 
    Public Property Articles As DbSet(Of Article) 
    Public Property Blogs As DbSet(Of Blog) 
End Class 

当我添加它创建控制器和视图控制器,然后运行,我得到这个错误:

The model backing the 'ArticleDbContext' 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 optionally seed it with new data.

Source Error:

Line 12: Line 13: Function Index() As ViewResult Line 14:
Dim articles = db.Articles.Include(Function(a) a.Blog) Line 15:
Return View(articles.ToList()) Line 16: End Function

Source File: C:\Users\darchual\documents\visual studio 2010\Projects\Blog\Blog\Controllers\ArticleController.vb
Line: 14

我该如何解决这个错误?

编辑:

我从数据库中删除的EdmMetaData表解决了这个。我需要这张桌子吗?对任何事物都有好处吗?

谢谢。

+0

错误消息似乎暗示了您2种可能的解决方法。当你尝试他们时发生了什么? – 2012-07-26 14:15:36

+0

感谢您的快速响应。我编辑了这个问题:“我通过从数据库中删除EdmMetaData表来解决这个问题,我需要这张表吗?它对任何事情都有好处吗?” – user1477388 2012-07-26 14:24:08

+0

EdmMetaData被实体框架用来验证当前模型是否与数据库匹配。删除它使得EF认为一切都是最新的,所以它将无法检测到未来的变化。 – 2012-07-26 22:09:00

回答

1

解决这个问题对我来说是从数据库中删除EdmMetaData表。

+0

如何?请详细说明。这个问题似乎也被不同的EF版本所困惑。 – jwrightmail 2012-10-17 04:02:50

+0

我通过右键单击数据库并单击删除从数据库中删除表。我不再使用CodeFirst方法,我只是创建数据库,然后编写一个模型来反映它。 – user1477388 2012-10-17 11:50:31

相关问题