简体   繁体   中英

ASP/C# from ADO to Entity Framework

I watched a few EF tutorials but I coulnd´t figure out a few things. I am currently working in a .net test project that uses ADO to interact with the SQL server database. I have to pass the ADO part to entity framework now. I am currently working with 3 layers, Business, object and DataAcess.

In DA I have the following query to update the Employee table from my website. It works, but I would like to pass this to Entity Framework using Linq. I have a few more cases like this with insert and select, but I´m sure I will be able to figure them out once I can sort this one.

Note: I already connected my DataAccess class with the database using Entity framework, and the entity list object is called ListEntities.

SqlConnection conn = new SqlConnection();
try
{  
    SqlCommand comm;
    string connectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;

    conn = new SqlConnection(connectionString);

    comm = new SqlCommand("UPDATE Employee SET Name=@Name, Lname=@Lname, Age=@Age, Email=@Email, Year=@Year, ColorID=@ColorID, AvatarID=@AvatarID WHERE EmployeeID=@EmployeeID", conn);
    comm.Parameters.Add("@Name",
    System.Data.SqlDbType.VarChar, 70);
    comm.Parameters["@Name"].Value = User.Name;

    comm.Parameters.Add("@Lname",
    System.Data.SqlDbType.VarChar, 70);
    comm.Parameters["@Lname"].Value = User.Lname;

    comm.Parameters.Add("@Age",
    System.Data.SqlDbType.Int);
    comm.Parameters["@Age"].Value = User.Age;

    comm.Parameters.Add("@Email",
    System.Data.SqlDbType.NVarChar, 70);
    comm.Parameters["@Email"].Value = User.Email;

    comm.Parameters.Add("@AYear",
    System.Data.SqlDbType.Int);
    comm.Parameters["@Year"].Value = User.Year;

    comm.Parameters.Add("@ColorID",
    System.Data.SqlDbType.Int);
    comm.Parameters["@ColorID"].Value = User.ColorID;

    comm.Parameters.Add("@AvatarID",
    System.Data.SqlDbType.Int);
    comm.Parameters["@AvatarID"].Value = User.AvatarID;

    comm.Parameters.Add("@EmployeeID",
    System.Data.SqlDbType.Int);
    comm.Parameters["@EmployeeID"].Value = User.UserID;

    conn.Open();
    comm.ExecuteNonQuery();
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    conn.Close();
}

I am not sure if i did understand what you said but I guess you want to translate this pure ADO you've mentioned to EntityFramework. I am not also sure what is the step you are in with EF in this example so I am going to list the whole steps.

1- You need to install the entityframework package (on the 3 layers) using the package manager

2- you need to create a context for example DataContext that extends from the DbContext

3- Create the DbSet Employee, And the class Employee with its properties

public class DataContext : DbContext
{
    public DbSet<Employee> Employees {get; set;}
}

4- And finally do an update

Employee emp = DataContext.Employees.FirstOrDefault(r=>r.EmployeeID == User.UserID);
emp.Name = User.Name;
emp.Age = User.Age;
...
DataContext.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM