Entity framework model creation - sql

When using the Entity Framework there are basically two ways to create your model. You either create the model in SQL server or in Visual Studio EF designer. Those are outlined below.
Start with Database
You first create the model in your SQL server DB then point EF to create the .edmx file for you. By using this approach you can use SQL server management studio to create all of your models and relationships.
Start with Visual Studio EF Designer
This approach is to create the model first in Visual Studio and from that create your database. By doing this it seems like you don't have to be soo concerned with tables and relationships.
Here is what I do and why I do it that way
I start by creating my model using SQL server management studio. I do this because I think its easier to create and modify tables using that tool, also I know exactly what is being created. I create my EF model by pointing it to my existing database. After that I create a Visual Studio Database Project so that my database is scripted into files which I put into version control. When I need to make changes, I change the database and then update my .edmx file as well as my database project.
I was wondering what are the pros and cons to these different approaches and what should be the criteria to decide which to use? Am I doing it wrong? Should I be creating my model first in Visual Studio?

I don't think that there's a 'right' or 'wrong' way to do this, a lot depends on how you deploy your code, where it goes to etc. There is also a third way, which Scott Guthrie blogged about recently:
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
As a side note, even if you start with the model designer, I think you always have to think about your tables/relationships, as getting these wrong in the database can cause you big problems further down the line.

I don't think there is a right or wrong way.
At our company we are developing the database changes directly first, apply them to the edmx model for existing models.
For new models, we create the edmx model first, then generate the database. From that point on we usually update the database directly. After we have tested our code internally and it runs correctly, and we know that our SQL database is correct (and of course prior to checking in), we'll then apply the changes to the database project by doing a SQL compare on the database to the database project.
This has worked very well for us.

Related

EF6 Schema compare between database and sql-file?

I use EF6 model-first approach to design MS SQL database by using "Generate database from model..." in Visual Studio.
This generates a sql-file with all drop and create statements.
At some productive stage where the database is filled with records, I do not want all tables to be dropped and recreated, their scheme should be updated in place without the need to backup and restore all records. That's where I found EF Migrations would be useful.
As I read, EF migrations are only suitable for code-first approach - so not what I need.
Next stop is the Schema Comparison Tool in Visual Studio, from which I think, it could fit.
But unfortunately I do not have 2 databases to compare, what I want to compare is the existing database to the new EF model (or the SQL-file generated out from that) to get a SQL-file for deploying only changes of tables.
Is this possible at all and if yes - where do I have to look at?
There isn't an EF tool for this from Microsoft, not like they built for SQL projects and dacpacs.
We had a similar need to ensure production schemas didn't differ to the point of runtime errors so I wrote one:
https://github.com/reckface/EntityFramework.Verify
It's covered in this Code review question. It doesn't generate any SQL to fix the differences, but does notify you of the differences.
try using Devart Entity Developer it's easy to use and let you compare EF models with Database. I've used it for over 3 years and works like charm.

Asp.net Database Migration for Database-First Approach

There are lots of tutorial for database migration when using code first approach with nuget. How can i migrate if i use database first approach in my MVC applicaion ?
You can't!
EF database migration only targets the code first approach. As you can see in the model designer in VS, when you right-click on the model, you have just one option about the update which is Update model from Database.
Another relative option is Generate database from model which I don't think that you need to use it!
However, you can change your approach to Code first and start to use all of its magic tools. To do so, see my answer here

Table not removed in EDMX after deleting in SQL

AM working with MVC4 VS2012 and EF5.
I created a EDMX using the DataBase and the POCO(TT) Classes were generated Automatically.
Followed this way Entity Framework 5 and Visual Studio 2012 POCO Classes in Different Project to move POCO to different Project.
Now i edit my datatype (Int to String or AllowNull) or delete my table in SQL Server. i come and update my model(EDMX). But changes are not reflected.
Am i doing wrong or missing something ?
Thanks
I don't think you are missing anything.
I'm unsure of the logic used by the designer when it decides what to update. From experience it seems to mostly add new. I haven't seen much updating.
I tend to just delete the table and add it again as the least painful route. This will remove any custom mapping, so it's not ideal if your entities are very different from your tables.

Database Schema To Another Database Schema Converter

Is there anyway that I could generate database from one schema to another?
For example, I would like to run my application from oracle to sql server or from sql server to postgresql?
I'm looking for something that's free....
Also I would like to know if there's a schema initializer to go with it
thanks a lot
The best tool I found so far is to use Squirrel's DBCOPY plugin. It really does the job
This doesn't really fall under the "free" category, but if you already have the tools (Microsoft Visual Studio and appropriate .NET providers), it might be a possibility.
The idea is to generate a data model from one database in Visual Studio and then use the Model First functionality to go from the conceptual model to another database. The steps would be something like this:
In a VS2010 application, add a new item to the project: ADO.NET Entity Data Model
Choose your existing database the source for this new model and select the desired tables and click through the wizard.
After the model is created, make sure the conceptual model is visible (.edmx file). In the properties for the model, there should be a DDL Generation Template option. Change this to the desired target type. This is the really iffy part; I don't know how many providers support this, but a quick search seemed to turn up at least one for Postgres.
Right click on the model and choose Generate Database from Model. This should produce the DDL for the new database.
This certainly would not lend itself well to an automated process, but for a one-time process, it might be okay.

How do you create a database from an EDM?

How do you create a database from an Entity Data Model.
So I created a database using the EDM Designer in VisualStudio 2008, and now I want to generate the SQL Server Schema to create storage in SQL Server.
From what I understand you are not just supposed to use EDM as a "pretty" database designer, in fact EDM does not depend on a specific storage layer. It tries to abstract that part for the developer. There are design schemas (CSDL) and storage schemas (SSDL). Anyway, don't mean to lecture you. ;)
There is EDM Generator, which you use to create models and class, etc.. For a DDL kind of export, I've never done that but what I did was map my EDM to an existing database, which was easier for me to get started.
There is a great tutorial on MSDN, which details step by step instructions on how to go about using an existing database, but also touches the how to start from scratch approach.
http://msdn.microsoft.com/en-us/magazine/cc163286.aspx
The Feature "Generate Database Schema from Model" is scheduled for a future release of Entity Framework. V1 does'nt support schema generatiorn based on EF models.
I believe the other answers implied this, but just to be explicit - use SSMS (or whatever equivlent if you're a brave sole and not using SQL Server provider) to design the DB layout and then suck that into EDM - and then apply application changes as necessary to the model.
I spent about an hour trying to do it your way first (leftover habit from some other Java ORM tools) - I eventually gave up and now do it the 'Right Way' (tm)
Eventually it would be nice (as JRoppert indicated) to have the generate databse schema from model feature - then you could get your DDLs for various DB flavours automagically.
Generating databases from model is a feature planned for vNext of Entity Framework.
Check out this blog post of Entity Framework Design explaining the planned features for database generation from a model.
What you must do right now is either 1) generate the database by hand, or 2) parse the CSDL file and write your own generator. I think option 1) is probably a better option.
Avilable in EF 4:
http://blogs.msdn.com/b/efdesign/archive/2008/09/10/model-first.aspx