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

1
  • 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. Commented Dec 19, 2012 at 18:55

1 Answer 1

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;
  }
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
Typically Entity Framework creates a Derived Version of DbContext. Using the base class will most likely not work.
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. :)
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...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.