Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,989 members, 7,821,447 topics. Date: Wednesday, 08 May 2024 at 01:05 PM

Entity Framework 4.1 - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Entity Framework 4.1 (1413 Views)

BB Z10 Applications Entity / Best PHP Framework For Application Development? / Visual Studio 2010 And .net Framework 4 (beta 2) Released (2) (3) (4)

(1) (Reply)

Entity Framework 4.1 by cocoon(m): 12:10pm On Aug 03, 2011
Lately I was helping a friend out on an a small app with EJB 3.1 (statelessbean) with jsf . Prior till then i was work on a Silverlight WCF RIA prism application with entity framewok 4.0. When i looked at the entity model in java which allows both code- first and model- first approach i tried to find the equivalent in .net entity framework, then i remembered that the code-first approach was missing in .net ,though ,there are other options like NHibernate ,etc. lately ago microsoft has come up with the latest version on the Entity Framework: Entity Framework 4.1 update which now has both the code-first approach and the model approach.

Some of the Features of Entity Framework 4.1

1. Code-First Approach : You write your classes and the database,tables will be created automatically for u

2.Relationtioships with the tables will automatically be created for u

3.now u write very little code to achieve alot.


Sample

assuming u have a class Student that u like to persist

public class Student
{
int MatricNo{get;set;}
string FirstName{get;set;}
string LastName{get;set;}
string CourseName{get;set;}
}


Step 2:

Add a reference to the EF 4.1 lib and create another class StudentDataContext that inherites from DbContext

public class StudentDataContent: DbContext
{
public DbSet<Student> students{get;set;}
}


Step 3:

Modify the app.config or web.config

<connectionStrings>

<add

name="StudentDataContent"

providerName="System.Data.SqlClient"

connectionString="Server=.;Database=StudentDb;Trusted_Connection=true;"/>

</connectionStrings>



step 4:

The code to persist

public class DbRoutine
{


public DbRoutine()
{
}


void SaveStudent()
{

using (var db = new StudentDataContent())

{

var student=new Student{FirstName="Ade",LastName="Ola",CourseName="Marketing"};

db.Students.Add(student);

db.SaveChanges();

}

}
}


After this just check ur sqlserver db the database and the schema would have been created with Matric No as the primary and indentity key.

Although there is still more work to be done by microsoft in other to make the EntityFramework a truly ORM .
(This short tutotial was written in haste pardon me for any error found)
Re: Entity Framework 4.1 by Beaf: 3:49am On Aug 10, 2011
Very nice and "to the point" article, thanks.

I've been using the entity framework with MySql recently (porting NopCommerce) and its a real pain having to wade through so much XML.
Microsoft could have achieved much more if they hadn't taken the XML route, so coders get caught out with funny errors (eg case sensitivity) that could have been better dealt with by resolving the model to proper C#, C++ or VB code.
Re: Entity Framework 4.1 by cocoon(m): 11:22am On Aug 10, 2011
I just remember that there was a little error ,this class

public class Student
{
int MatricNo{get;set;}
string FirstName{get;set;}
string LastName{get;set;}
string CourseName{get;set;}
}

should have been

public class Student
{
int Id{get;set;}
string FirstName{get;set;}
string LastName{get;set;}
string CourseName{get;set;}
}


meaning Id and not matricNo
when u use matricNo and compile,it will not compile and it will prompt u for an ID, as in, something like a primary key to uniquely identify the table Students

(1) (Reply)

Are There Any Haskell Programmers Here / Science: Could You Travel Back In Time? See What Scientists Achieved. / Windows 8 Activator Free Download Latest Working [pics]

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.