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.
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.
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.
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.
EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.
There are three database initializers that are included with entity framework they all implement IDatabaseInitializer<Context>
interface. They are:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With