2
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ Providing tutorials by themselves is not what we do here. Answerers can always recommend some along with a code review. \$\endgroup\$
    – Jamal
    Mar 3, 2015 at 6:17

1 Answer 1

1
\$\begingroup\$

Your mapping is designed to call a stored procedure when you call context.Applicants.Add() and context.SaveChanges(). Your second code manually calls a stored procedure, that is not what you want to do. Simply keep your mapping:

using(var context = new MyExperienceDbContext())
{
    context.Applicants.Add(entity);
    context.SaveContext();
}

Also this code does not belong in your controller, it belongs in your repository.

public interface IApplicantRepository
{
    void AddApplicant(Applicant applicant);
}

For a more advanced look at Repositories please read the first part of my article: http://www.lind-i.com/Docs/DataSelectorsWithMVVM.pdf

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.