NeoDeveloper
NeoDeveloper

Reputation: 61

EF 5 & MVC 4: unable to get tables with no reference in Controller using Databse first

I am using MVC 4, EF 5 and Database first approach. I have added 2 new tables(UserDetails and UserAddress) with no reference in existing database. I am able to see all tables including these 2 new table in EDMX file and created model also. But when im trying to get the tables in controller, its showing all Models except UserDetails and UserAddress - in following way.

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
  private DbRestaurantEntities db = new DbRestaurantEntities();

   public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    var userId = WebSecurity.GetUserId(User.Identity.Name);
                    //db.UserDetails -- Not showing
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
            return View(model);
        }
}

Am i missing something?

Upvotes: 0

Views: 156

Answers (1)

Ravi Gadag
Ravi Gadag

Reputation: 15861

WebSecurity uses different dbContext , you need to use the same dbContext, which is used to create the database tables. you can check it out in the web.config file for the connectionstring.. and use the exact dbContext

Add this in AuthConfig.cs

WebSecurity.InitializeDatabaseConnection(
        "specifyTheDbContextHere",
        "UserProfile", 
        "UserId", 
        "UserName", 
        autoCreateTables: true
    );

Upvotes: 1

Related Questions