0

First off, let's start by saying I'm a rookie. That being said, I am building a web app in C# using MVC3 w/ EF4.3.1 using a db first approach. During the log on process, I want to set some session variables to be used throughout the app.

My issue is that I am not sure how to go about reading the values into the session variables. Below is the code currently in the AccountController.cs:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, 
                                              model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) 
                && returnUrl.Length > 1 
                && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") 
                && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", 
                "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

And from what I have read, I should be trying to use DbContext to access the property of a given field, similar to:

using (var context = new DbContext())
{
    var svc100 = context.SVC00100.Where(t => 
                        t.TECHEMAIL.Contains("model.UserName")

    // Read the current value of the Name property
    Session["Name"] = context.Entry(svc100).Property(u =>
                                                 u.Name).CurrentValue;
}

Now... How do I determine what should be used in place of DbContext() in the second code block? If I try to use the drop down and pick what I need, I do not see anything relating to my Entity (CPLUEntities) that contains the DbContext phrase.

Some of the links I've discovered:

Implement custom “ValidateUser” in MembershipProvider

Entity Framework Working with Property Values

Using DbContext in EF 4.1 Part 5: Working with Property Values

Community
  • 1
  • 1
Phillip O.
  • 135
  • 3
  • 10
  • How did you start? At some point you must have created an EF model, probably containing a class that is derived from ObjectContext or DbContext. Try to find that class and use it in your `using` statement. – Gert Arnold Dec 19 '12 at 18:55

1 Answers1

2
using (var context = new DbContext())
  {
     var svc100 = context.SVC00100
                 .FirstOrDefault(t => t.TECHEMAIL.Contains(model.UserName));

     // Read the current value of the Name property
     if (svc100 != null && !String.IsNullOrEmpty(svc100.Name))
         Session["Name"] = svc100.Name;
  }
Scott
  • 13,735
  • 20
  • 94
  • 152
  • Scott, when I tried using the first line in that code block `using (var context = new DbContext())` I get an error about _'System.Data.Entity.DbContext.DbContext()' is inaccessible due to its protection level_ that's where I KNOW I am stuck... I can't figure out what should be in place of **DbContext()**. I wouldn't be surprised if I do need to make the modifications you suggested, though. – Phillip O. Dec 19 '12 at 18:38
  • Open the class file for DbContext(). You should see an access modified before the class name, something like "public partial class DbContext". If its say anything other than 'public' (like protected, private or internal), try changing it to 'public' and see if that fixes the problem. Without seeing your code though, might be tough. – Scott Dec 19 '12 at 18:40
  • Typically Entity Framework creates a Derived Version of DbContext. Using the base class will most likely not work. – Erik Philips Dec 19 '12 at 18:50
  • I figured the OP was using DbContext as a generic reference to whatever his context name was. Guess I should have been more specific about that. :) – Scott Dec 19 '12 at 18:55
  • Well, that seems to have gotten me close to what I need. The code no longer returns an error. Now I need to figure out why the session variables are empty... – Phillip O. Dec 20 '12 at 13:24