Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework not creating tables

I've been using EF for a while now, always in the Model-First approach. Now I'm adventuring through Code-First lands. The thing is: I've been having issues with automatic table creation.

According to some sites it is possible. And I've tried their approach with no success.

Here's one of the things I've tried: Database.CreateIfNotExists()

No luck...

My connection string is perfect and working. If I add the table manually it does work. The problem is when I don't have the table created. It just doesn't create as I was said it would.

My classes are correctly decorated. (Again: It's working when I have the DBs created)

Any suggestions? Does this feature really works?

I'm using:

Visual Studio 2010 Professional

EntityFramework 4.3.1 (although I tried with 4.1 also)

SQL Server 2008 R2

Thanks in advance.

like image 437
eestein Avatar asked Apr 03 '12 01:04

eestein


People also ask

Does Entity Framework create tables?

If you're using a Code First approach then Entity Framework will build the table for you. It looks like you are not using Code First, so you will have create the table in the database.

How do I create a table in EDMX?

Open edmx file, right click anywhere in the modal and select "Update Model from Database" and follow the instructions and choose the desired tables and SPs. Sometimes even after following these steps, your model will not get updated, close Visual Studio and restart it again.

Should I use EF core or EF6?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Is EF core faster than EF6?

EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.


1 Answers

There are three database initializers that are included with entity framework they all implement IDatabaseInitializer<Context> interface. They are:

  • CreateDatabaseIfNotExist (default)
  • DropCreateDatabaseWhenModelChanges
  • DropCreateDatabaseAlways

As you see the default API does not have the initializer that just creates tables, instead it does the whole database. However there are other initializers that people have created, there is one that does exact same thing that you want.

It's in the Nuget package EFCodeFirst.CreateTablesOnly

Another option is to create your own initializer if that's something you really need.

like image 197
Sergey Avatar answered Oct 15 '22 07:10

Sergey