Asp mvc 3 noobie: Why is the code-first method not

2019-06-27 05:15发布

问题:

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.

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'.

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).

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

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

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

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

What am I missing?

回答1:

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

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.

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



回答2:

You might want to look into this article, same question successfully answered already. Or it can be this (also resolved successfully) Answer to your problem is most likely one of the two.

Hope this will help you



回答3:

Does the name you're specifying for your connection string match the name of your database context?

For example:

Context

var myDbContext = new MyDbContext();

Connection string

<connectionStrings> 
    <add name="MyDbContext" connectionString="YOUR.CONNECTION.STRING" providerName="System.Data.SqlServer" /> 
</connectionStrings>


回答4:

Try and see if this post I wrote about DbContext with MVC works for you: Code-First

Not a lot to be done to get this to work, but there are a few things that are easily missed that will cause a bunch of head aches.

hope this helps



回答5:

I had already created a database with that name on SQL server. Once I deleted the existing database, the code first framework created the tables for me like it was supposed to. It seems like if the database already exists, the framework won't set up the tables for you. It wants to create the whole DB from scratch.



回答6:

You were using AdventureWorks Database?

It has it's own schema assigned to the employees table. HumanResources.Employees and not the default dbo.Employees.

Even though I've identified the problem, I don't know the solution to using the database as configured with the HumanResources schema.

Anybody know?