Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.09/5 (4 votes)
See more:
Hi Everybody,

My Database is in SQL Server and it is already working fine with Asp.Net site by using Connection String ( SQL Client ) but i want to implement it to an architectured way and multipurpose way.
So I have some question Regarding That.

Should i need to Use Entity Framework on Project??
Should I need to Use Entity Framework in WCF web service then access web service in Project??

Pls suggest me how to use database with the project.
Posted

If you are trying to build a database independent library, the Enterprise Library Data Access Application[^] could help you.
 
Share this answer
 
Yes, You can use entity framework. all of you database operation you can use separate Project (Data Access Layer) in the application.
 
Share this answer
 
v2
If I'm correct from your question I understood that u might be using ADO.NET directly for connecting to database

if u are using ADO.Net

C#
public IEnumerable<instructor> GetInstructorsADONET()
{
    var connString = ConfigurationManager.ConnectionStrings["SchoolContext"].ConnectionString;
    List<instructor> instructors = new List<instructor>();
    using (var conn = new SqlConnection(connString))
    {
        conn.Open();

        var cmd = new SqlCommand("SELECT Person.PersonID, Person.LastName, Person.FirstName, Course.Title, Course.Credits " +
            "FROM Course INNER JOIN " +
                  "CourseInstructor ON Course.CourseID = CourseInstructor.CourseID RIGHT OUTER JOIN " +
                  "Person ON CourseInstructor.PersonID = Person.PersonID " +
                  "WHERE (Person.Discriminator = 'Instructor') " +
                  "ORDER BY Person.PersonID", conn);
        var reader = cmd.ExecuteReader();

        int currentPersonID = 0;
        Instructor currentInstructor = null;
        while (reader.Read())
        {
            var personID = Convert.ToInt32(reader["PersonID"]);
            if (personID != currentPersonID)
            {
                currentPersonID = personID;
                if (currentInstructor != null)
                {
                    instructors.Add(currentInstructor);
                }
                currentInstructor = new Instructor();
                currentInstructor.LastName = reader["LastName"].ToString();
                currentInstructor.FirstMidName = reader["FirstName"].ToString();
            }
            if (reader["Title"] != DBNull.Value)
            {
                var course = new Course();
                course.Title = reader["Title"].ToString();
                course.Credits = Convert.ToInt32(reader["Credits"]);
                currentInstructor.Courses.Add(course);
            }
        }
        if (currentInstructor != null)
        {
            instructors.Add(currentInstructor);
        }

        reader.Close();
        cmd.Dispose();
    }
    return instructors;
}</instructor></instructor></instructor>



To avoid more code to be written and tested you can use ORM(object Relational Mapping)

C#
public IEnumerable<Instructor> GetInstructors()
{
    List<Instructor> instructors;
    using (var db = new SchoolContext())
    {
        instructors = db.Instructors.Include("Courses").ToList();
    }
    return instructors;
}



Its not compulsory to use Entity framework depending upon your application and for any plans to migrate the data.


Refer this links

http://msdn.microsoft.com/en-us/library/ms178359(v=vs.110).aspx#orm[^]

http://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900