I'm a beginner in MVC programming, repository, and UOW using Entity framework. I tried to calling my existing SP in my MVC project using UOW and EF.
Here is my code to mapping my SP in myDbContext file:
modelBuilder.Entity<Applicant>().MapToStoredProcedures(
s => s.Insert(
i => i.HasName("InsertApplicant")
.Parameter(fn => fn.First_Name,"FirstName")
.Parameter(ln => ln.Last_Name,"LastName")
.Parameter(em => em.Email,"Email")
.Parameter(tn => tn.Tel_No,"TelNo")
.Parameter(mn => mn.Mobile_No,"MobileNo")
.Parameter(vt => vt.Visa_Type,"VisaType")
.Parameter(lu => lu.LinkedIn_URL,"LinkedInURL")
.Parameter(ob => ob.Objective, "Objective")
.Parameter(ac => ac.Active, "Active")));
And, below is the code I used to call the SP on my controller (i don't know if it's right or not):
using (var context = new MyExperienceDbContext())
{
var fn = new SqlParameter("@FirstName", entity.First_Name);
var ln = new SqlParameter("@LastName", entity.Last_Name);
var em = new SqlParameter("@Email", entity.Email);
var pn = new SqlParameter("@TelNo", entity.Tel_No);
var mn = new SqlParameter("@MobileNo", entity.Mobile_No);
var vt = new SqlParameter("@VisaType", entity.Visa_Type);
var lu = new SqlParameter("@LinkedInURL", entity.LinkedIn_URL);
var ob = new SqlParameter("@Objective", entity.Objective);
var ac = new SqlParameter("@Active", entity.Active);
context.Database.ExecuteSqlCommand("InsertApplicant @FirstName,@LastName,@Email,@TelNo,@MobileNo,@VisaType,@LinkedInURL,@Objective,@Active ",
fn, ln, em, pn, mn, vt, lu, ob, ac);
return RedirectToAction("Index");
}
It works fine for me, but I noticed if I delete the mapping's code in myDbContext
file everything still works fine. I believe i'm failed to implemented the UOW and repository concept in EF when calling my existing SP, right?
Do you have any idea with my question above or do you have tutorial suggestion to calls SP using UOW and EF in MVC project?