Entity Framework Exception: Invalid object name

前端 未结 6 686
故里飘歌
故里飘歌 2020-12-15 04:09

I am trying to create database using Code First approach. When I run the following code I am getting the following exception. Is there anything wrong in the fields that I de

相关标签:
6条回答
  • 2020-12-15 04:31

    As I understood, you are expecting the code to create DB automatically based on your entities description. But this will not happen unless you create DB explicitly. Please check the following link http://www.simple-talk.com/dotnet/.net-framework/entity-framework-4460---learn-to-create-databases-from-the-model/ and this tutorial on EF codefirst: http://codefirst.codeplex.com/

    0 讨论(0)
  • 2020-12-15 04:38

    I've just ran into the exact same issue - I'd already created my database on a development SQL box inside our network that needs SQL authentication.

    When I ran my app, no tables were created. I found this awesome but simple article about creating a Code First Database Initializer Strategy which first checks to see if the database exists and then runs a script against the database to create the tables.

    As stated in the article - pay attention that when such a strategy is deployed, whenever the application starts over, all the database tables will be recreated! This strategy should only run once.

    But you already knew that.

    0 讨论(0)
  • 2020-12-15 04:39

    @Slauma's answer is the right one - the tables are created upon initialization. It's probably easiest to just delete the database and let EF create it (if you leave your connection string as is, it will create a database called LibraryReservationSystem on the local machine; you should probably specify an explicit host name if you're going to use the connection string in the config at this point).

    You would need something along the lines of:

    public class NerdDinnersInitializer : DropCreateDatabaseIfModelChanges<NerdDinners> { }
    

    And you would also need to set the initializer in your Main method:

    Database.SetInitializer(new NerdDinnersInitializer());
    

    Word to the wise: NEVER deploy an application with an initializer like the one above. You can see this blog post about how to control initializers via the config file for more details on how to control this in production applications.

    0 讨论(0)
  • 2020-12-15 04:40

    As the error suggests, you do not have a table called Dinners within your database.

    Are you using Single or Plural table names? i.e. Dinner or Dinners?

    0 讨论(0)
  • 2020-12-15 04:42

    Once you have your entities designed you can right-click the workspace of the EDMX file and select 'Generate Database from Model...'. Click through until you have a script in your window. For that script you will have to remove DB creation step (if it is there) and go straight for CREATE TABLE ...

    Copy-Paste and execute in whatever DB you've got. You might have to adjust the script for a specific RDBMS.

    0 讨论(0)
  • 2020-12-15 04:48

    The LibraryReservationSystem database is already existing database. It has no tables. I am expecting EF to create the tables.

    That's not correct. If the database exists EF doesn't create any tables in this database. EF can create the database if it doesn't exist. That is the default database initializer CreateDatabaseIfNotExists that gets applied if you don't change it explicitly. You can select two other initializers: DropCreateDatabaseAlways or DropCreateDatabaseIfModelChanges. But neither of those will only create tables in an existing database but instead delete the database completely and create it from scratch including all tables.

    What can you do:

    • Either delete the database manually (in SSMS for example), then EF will create a new one including the tables
    • Or use the DropCreateDatabaseAlways initializer once to let EF create the database including the tables, then remove the initializer again
    • Or if you can't delete the database for whatever reason write SQL code in the Seed method that adds the tables to the database (Wrong, thanks to Mark Stafford's comment)
    • Or use Code-First Migrations (EF >= 4.3) to add new tables to an existing database when you have added new entities.
    0 讨论(0)
提交回复
热议问题