1

I am trying to access the DB I created to display a list. I am doing this in my controller. It is red underlining the db part. It is saying "The name db does not exist in the current context." Am I missing some thing to connect to the db in my code?

using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Helpers;
using MIS424Assignments.Models;

namespace MIS424Assignments.Controllers
{
    [Authorize (Users="[email protected]")]
    public class RetailController : Controller
    {
        // GET: Retail
        [AllowAnonymous]
        public ActionResult Index()
        {             
                string sql = "Select * from Product order by newid()";
                List<Product> productList = db.Product.SqlQuery(sql).ToList();
                return View();
        }
    }
}
3
  • Can you provide the errors you are receiving? Commented Apr 14, 2017 at 16:52
  • You will have to execute the query under your DBContext. Learn more about Entity framework here entityframeworktutorial.net/EntityFramework4.3/… Commented Apr 14, 2017 at 16:56
  • 1
    You have not initialized db. Please initialize your database to db and try. Post your result so that we can answer you better. Commented Apr 14, 2017 at 17:00

3 Answers 3

2

Try this

[Authorize (Users="[email protected]")]
public class RetailController : Controller
{

     private readonly RetailStoreEntities1 db;
    public RetailController()
    {
        db = new RetailStoreEntities1();
    }

    // GET: Retail
    [AllowAnonymous]
    public ActionResult Index()
    {             
            string sql = "Select * from Product order by newid()";
            List<Product> productList = db.Product.SqlQuery(sql).ToList();
            return View();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Somebody here is bound to suggest to do all your DB Access from a repository, and invoke the repository methods from your controller. However, to quickly answer your question. Your project should include a class with your context

public class MyContext: DbContext
{
public MyContext()
     : base("myconnstringinwebconfig")
{}
//more stuff here, maybe your entities.    
}

and somewhere near the top of your controller, there should be a line that will initialize your context:

private MyContext db = new MyContext();

and that will fix your error message.

Comments

0

I don't see the connection string. Could that be the problem? If you are using EF, did you configured the connection string in your webconfig.

I used to get burned on that one when I first started with EF :-D

***UPDATE **** The code below would go within your Context Class as the constructing sub.

Let me know if it helps

public YourContextClass() : base("RetailStoreEntities1") {

}

4 Comments

I have <add name="RetailStoreEntities1" connectionString="metadata=res://*/RetailStoreModel.csdl|res://*/RetailStoreModel.ssdl|res://*/RetailStoreModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\RetailStore.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /> in my web config
Are you passing the ConnectionString name "RetailStoreEntities1" to your DB Context?
I will append a sample to my original post ;-)
@BasicIsaac answer is in a nutshell :-)

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.