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?
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
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.
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.
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.
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?
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.
Just my opinions on why someone might want to use code first:
If your database is not too complex then you have an easy to read set of files and classes that show what is in your database.
edmx files generate a lot of code that a developer may or may not be able to follow. With code first the code you write is all the code you need to check into source control
Large edmx files can be difficult to manage in the designer. Edmx files can take a while to open. Code First classes are just class files so they open and close super fast. You can go in, make a small change and save in a matter of seconds.
In my experience edmx files are easier to break. A connection string somehow changed, a table or filed accidentally removed.
Some developers just don't like code generated for them.
With code first you don't need the edmx designer in order to make changes to your model. You don't even need VS.net if you are using it in a non project type of application (web site).
Code First plays well with source control and you can easily see what has changed between one changeset and another.
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.
JoeReynolds
Participant
871 Points
313 Posts
Why Code First with Existing DB?
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?
EnenDaveyBoy
Participant
1465 Points
1146 Posts
Re: Why Code First with Existing DB?
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
http://msdn.microsoft.com/en-us/data/gg685489
jsiahaan
Contributor
2304 Points
588 Posts
Re: Why Code First with Existing DB?
Apr 09, 2012 12:30 AM|LINK
Hi,
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:
http://forums.asp.net/t/1789430.aspx/1?what+are+advantages+in+model+first+and+code+first+as+compare+to+Database+First+approach+in+EF+
Have fun
Indonesian Humanitarian Foundation
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Why Code First with Existing DB?
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.
Blog | Twitter : @Hattan
akfkmupiwu
Member
114 Points
63 Posts
Re: Why Code First with Existing DB?
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.
JoeReynolds
Participant
871 Points
313 Posts
Re: Why Code First with Existing DB?
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?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Why Code First with Existing DB?
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.
Blog | Twitter : @Hattan
beetledev
Member
750 Points
173 Posts
Re: Why Code First with Existing DB?
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.
JoeReynolds
Participant
871 Points
313 Posts
Re: Why Code First with Existing DB?
Apr 09, 2012 07:49 PM|LINK
Thanks to all. Got it.