Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 09, 2012 07:49 PM by JoeReynolds
Apr 08, 2012 11:42 PM|LINK
I'm currently using Entity Framework 4.1 in an MVC 3 application with a edmx file.
With all the talk about using Code First, especially on an existing database, I'm not sure of the advantages to stop using the edmx approach and convert to code first approach.
Could someone explain the real/measurable advantages of implementing code first on an existing database over simply using the EF edmx file approach?
Apr 09, 2012 12:19 AM|LINK
There are three versions, code first, model first and database first, you can use the one that suits your situation best, but for tutorials you have to sreach for the one you want, and it can be a pain finding them
Apr 09, 2012 12:30 AM|LINK
As a developer we need to know all of them we use one of them base on condition we have. Say you have a database already either create by ourself or from other partiy, we better use database first. If we design a database application from scratch it is good
to use code first, here we can manage the database creation on our own code. Actually it's a choice.
For more detail look at this thread:
Apr 09, 2012 01:38 AM|LINK
The main difference is whether you want to use EF entity objects, or poco (plain old class objects) as models. The classes generated by the edmx file contain all kinds of extra properties and methods that relate to change tracking and other things. While
a code first model is clean, it's just the properties you define.
So it really depends on your architecture and how you want to design your system and in particular your business objects. For some the edmx entities work and provide a nice mapping to the database, for others they might want to reuse those same models outside
of a database context and in that case code first is a better fit. Also, from my experience code first tends to be easier to unit test.
Apr 09, 2012 01:28 PM|LINK
When you have database already designed then use EF to generate Model and Context (edmx) .i.e. Database first. There is no benefit in using Code First, as you will end up in re-writing model code which can be generated automatic from your existing Database.
Apr 09, 2012 01:44 PM|LINK
Thanks for the responses. I still can't get my head around WHY I would want to stop using the edmx file approach for an existing database in use for 10 years and switch to code first. What would this accomplish this for me and why would
I want to make the switch?
Apr 09, 2012 02:42 PM|LINK
As I mentioned in my earlier post. The difference lies in the type of entities being created. Edmx files generate enttities while code first uses poco classes. The modesl generated by the edmx file contain a lot of pre-generated code to handle change tracking,
concurency, etc. In some architectures, you might not want models that contain all that extra stuff. An architect modelling a system may decide that poco models are easier to work with and do not tie the business entities to a specific technology. With edmx
models, I HAVE to use them with the entity framework, trying to get them to work with another technology could lead to problems. Now with code first, it's just plain clases. Meaning, I can save them using EF code first most of the time, but let's say I need
something different on a specific action, let's say I want to use dapper for faster data access. I can do that without changing my models, the same classes can be used. Also, let's say I want to serialize my data to xml, if I were to serialize the edmx models
I might get all the extra properties, with code first again, I can do that since it's just a pure class.
So it all comes down to the application, it's architecture and what you expect to be able to do.
Apr 09, 2012 04:59 PM|LINK
Just my opinions on why someone might want to use code first:
I'm not saying that you can't do all the above with edmx files, but in my experience using code first can be a lot easier.
Apr 09, 2012 07:49 PM|LINK
Thanks to all. Got it.