ASP.NET MVC4 Setting up DB Context and Models

asp.net-mvc asp.net-mvc-4 c# dbcontext entity-framework

Question

I am working with an ASP.NET MVC4 project (school project, so I am learning the framework), trying to get working entity framework 6.0 with a Code-first perspective. I created my models (/Models/) and the DbContext like this:

using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Autokereskedes.Models
{
    public class AutoDb : DbContext
    {
        public DbSet<Car> Cars { get; set; }
        public DbSet<Reservation> Reservations { get; set; }
        public DbSet<Depo> Depos { get; set; }
        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            base.OnModelCreating(modelBuilder);
        }
    }
}

But when I want to use it in my controllers I get an error

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Autokereskedes.Models;

namespace Autokereskedes.Controllers
{
    public class AccountController : Controller
    {

......

private bool IsUserDataValid(string email, string password)
{
    bool r = false;
    var crypto = new SimpleCrypto.PBKDF2();
    using (var d = new AutoDb())
    {
        var user = new d.SystemUsers.FirstOrDefault( u => u.email == email);
        if (user != null && user.Password == crypto.Compute(password))
        {
            r = true;
        }
    }

    return r;
}

The error is:

Error   1   The type or namespace name 'd' could not be found (are you missing a using directive or an assembly reference?) C:\_temp\stackoverflow\Autokereskedes\Controllers\AccountController.cs  64  32  Autokereskedes

Also, I can't really get working the models, I think I made mistakes when I coded the table relations (my tables have mostly One to Many associations), still when I used the models, I got foreign key errors (HomeController).

My whole project accessible here: download

Thank you for your time and help, I am quite lost here, browsing tutorials but still missing the point.

1
0
10/27/2013 11:11:45 AM

Accepted Answer

.NET thinks you are trying to instantiate a class called FirstOrDefault in the namespace d.SystemUsers, while you are merely trying to reference the SystemUsers property of the d object you just made..

So try removing "new":

using (var d = new AutoDb())
{
    var user = d.SystemUsers.FirstOrDefault( u => u.email == email);
    if (user != null && user.Password == crypto.Compute(password))
    {
        r = true;
    }
}
0
10/27/2013 11:34:24 AM

Popular Answer

May be try this

 var user = d.Users.ToList().FirstOrDefault(u => u.Email == email);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow