ASP MVC 3 noobie:为什么不是我建的SQL Server数据库上的code-第一种方法? [英] Asp mvc 3 noobie: Why is the code-first method not building my DB on sql server?

查看:110
本文介绍了ASP MVC 3 noobie:为什么不是我建的SQL Server数据库上的code-第一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个ASP MVC 3 noobie谁做了一些教程。现在我想建立一个网站。所有Microsoft网站上的教程强调code-第一种方法:你定义code模型,然后创建一个DataContext,然后实体框架创建/根据您的code管理数据库。

I am an ASP MVC 3 noobie who has done a few tutorials. Now I'm trying to build a site. All of the tutorials on the microsoft website emphasize the code-first approach: you define your model with code and then create a datacontext and then the entity framework creates/manages the DB based on your code.

我建立了一个员工类,并从的DbContext继承DataBaseContext类。我添加了一个连接字符串的Web.config连接字符串连接成功DataBaseContext 以一个已经存在的空数据库的SQL服务器上。 编辑=这是问题。见我的回答如下

I set up an Employees class and a DataBaseContext class that inherits from DbContext. I added a connection string to Web.config connection string that successfully links DataBaseContext to an already existing empty DB on SQL server. EDIT= That was the problem. See my answer below

但是当我尝试运行直通脚手架创建雇员控制器,我得到这个错误

But when I try to run the Employees controller created thru scaffolding, I get this error

Invalid object name 'dbo.Employees'. 
Description: An unhandled exception occurred during the execution of...
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Employees'.

我跟着这个帖子<一个href=\"http://stackoverflow.com/questions/5374482/sqlexception-0x80131904-invalid-object-name-dbo-categories\">SqlException (0x80131904):无效的对象名称dbo.Categories'并意识到如果我在DB创建一个员工表,这个错误时抛出消失(我得到一个新的话说,列名无效)。

I followed this post SqlException (0x80131904): Invalid object name 'dbo.Categories' and realized that if I create an employees table on the DB, this excpetion goes away (I get a new one saying that the column names are invalid).

但我认为MVC 3的整点是,你基于code中的架构将使数据库。

But I thought the whole point of MVC 3 is that the framework will make the DB for you based on the code.

也许我需要在Global.asax的Application_Start()一行code来创建数据库?这里是我的Application_Start方法中:

Maybe I need a line of code in the Global.asax Application_start() to create the database? Here is my application_start method:

Sub Application_Start()
    AreaRegistration.RegisterAllAreas()
    RegisterGlobalFilters(GlobalFilters.Filters)
    RegisterRoutes(RouteTable.Routes)
End Sub

下面是code的员工:

Here is the code for Employee:

Public Class Employee

    Property EmployeeID As Integer
    Property First As String
    Property Last As String
    Property StartDate As DateTime
    Property VacationHours As Integer
    Property DateOfBirth As DateTime 'in case two employees have the same name

End Class

下面是code为DB背景:

Here is the code for the DB context:

Imports System.Data.Entity

Public Class DatabaseContext
    Inherits DbContext

    Public Property Employee As DbSet(Of Employee)
    Public Property AnnualLeave As DbSet(Of AnnualLeave)

End Class

我是什么失踪?

推荐答案

在默认情况下使用EF DropCreateDatabaseIfModelChanges&LT; TContext&GT; 数据库初始化。因此到 MSDN

By default EF uses DropCreateDatabaseIfModelChanges<TContext> database initializer. Accordingly to the MSDN:

IDatabaseInitializer&LT的实现; TContext&GT; 将删除,重新创建,和可选的只有自数据库创建的模型已更改重新播种用数据的数据库。这是由创建时编写店模式到数据库中的哈希值,然后将该哈希比较一个从当前模型生成的实现。

An implementation of IDatabaseInitializer<TContext> that will delete, recreate, and optionally re-seed the database with data only if the model has changed since the database was created. This is achieved by writing a hash of the store model to the database when it is created and then comparing that hash with one generated from the current model.

由于数据库是手动创建,EF找不到散列并决定不进行任何进一步的初始化逻辑。

Since the database was created manually, EF can't find the hash and decides do not perform any further initialization logic.

这篇关于ASP MVC 3 noobie:为什么不是我建的SQL Server数据库上的code-第一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆