Skip to main content

Posts

Showing posts from April, 2019

Introduction to Entity Framework

Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.  It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored. It eliminates the    ADO.NET code that we write to connect to Database,Execute Query and read data from DataSet and convert to .net objects and vice versa. It Reduces the pain of developers and provides higher level of abstraction for faster development of .net applications.

Adding User To Role Using Identity

First Create a viewmodel  public class ApplicationUserRoleViewModel     {         public string UserId { get; set; }         public string RoleId { get; set; }       } Create an Empty Controller with name UserAndRolesConroller and add the following actions   public async Task<ActionResult> AddUserToRole()         {             ApplicationUserRoleViewModel vm = new ApplicationUserRoleViewModel();             var Users = await dbCon.Users.ToListAsync();             var roles = await dbCon.Roles.ToListAsync();             ViewBag.UserId = new SelectList(Users, "Id", "UserName");             ViewBag.RoleId = new SelectList(roles, "Id", "Name");             return View(vm);         }         [HttpPost]         public async Task<ActionResult> AddUserToRole(ApplicationUserRoleViewModel model)         {             var role = dbCon.Roles.Find(model.RoleId);             if (role != null)             {            

Converting DataTable to List

Calling from Controller  public ActionResult Index()         {             //join use             //change view model.             String sql = "SELECT * FROM faculties ";             db.List(sql);             var dt = db.List(sql);             var model = new Faculty().List(dt);             return View(model);         } Add a method to class List<Faculty> list = new List<Faculty>();         public List<Faculty> List(DataTable dt)         {             for (int i = 0; i < dt.Rows.Count; i++)             {                 Faculty fac = new Faculty();                 fac.FacultyId = Convert.ToInt32(dt.Rows[i]["FacultyId"]);                 fac.Name = dt.Rows[i]["Name"].ToString();                 fac.Description = dt.Rows[i]["Description"].ToString();                 list.Add(fac);             }             return list;         }

Using SqlDataAdapter to fill DataTable in c#

public DataTable List(string sql)         {             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);             SqlCommand cmd = new SqlCommand(sql, con);             DataTable dt = new DataTable();             SqlDataAdapter da = new SqlDataAdapter(cmd);             try             {                              con.Open();                 da.Fill(dt);             }             finally             {                 con.Close();             }             return dt;         }