locked
null on db.Products2.ToList() asp.net mvc RRS feed

  • Question

  • User1941901171 posted

    Hi, I´m using VS 2013 and sql 2014. I´ve created a simple application using EF data base first, it works, later I´ve deleted the model and I´ve created a new one, I´ve modified too the web config, but now I get no data and an error comes out: null on db.Products2.ToList()

    Data base table

    ProductsController

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using FirstMVC.Models;
    
    namespace FirstMVC.Controllers
    {
        public class ProductsController : Controller
        {
            private FirstMVC_dbEntities2 db = new FirstMVC_dbEntities2();
    
            // GET: Products
            public ActionResult Index()
            {
                return View(db.Products2.ToList());
            }

    The model: Products2

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace FirstMVC.Models
    {
        public class Products2
        {
            public int ID { get; set; }
            public string Description { get; set; }
            public decimal Price { get; set; }
            public System.DateTime LastBuy { get; set; }
            public double Stock { get; set; }
        }
    }

    The context FirstMVC_dbEntities2

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    
    namespace FirstMVC.Models
    {
        public class FirstMVC_dbEntities2
        {
            /*public FirstMVC_dbEntities2()
                : base("name=FirstMVC_dbEntities2")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }  */
        
            public virtual DbSet<Products2> Products2 { get; set; }
        }
    }

    Tuesday, July 17, 2018 2:55 AM

Answers

  • User-369506445 posted

    don't worry, for find the problem, please do below step

    you have a model below like

    public class Products2
        {
            public int ID { get; set; }
            public string Description { get; set; }
            public decimal Price { get; set; }
            public System.DateTime LastBuy { get; set; }
            public double Stock { get; set; }
        }

    now change your context to below like :

    public class FirstMVC_dbEntities2: DbContext
        {
            public FirstMVC_dbEntities2()
                : base(@"Data Source=JHON-PC\SQLEXPRESS;Initial Catalog=FirstMVC_db;trusted_connection=true")
            {
            }
        
            public virtual DbSet<Products2> Products2 { get; set; }
        }

    now in your controller

    private FirstMVC_dbEntities2 db = new FirstMVC_dbEntities2();
            public ActionResult Index()
            {
                var model = db.Products2.ToList();
                return View(model);
             
            }

    and put a breakpoint in the highlighted line

    Finally, tell me your feedback.

    note: please exactly use my code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 6:14 AM

All replies

  • User-369506445 posted

    hi

    your context class is wrong, you have to Inheritance the DbContext and define your connection String

    public class FirstMVC_dbEntities2
        {
            /*public FirstMVC_dbEntities2()
                : base("name=FirstMVC_dbEntities2")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }  */
        
            public virtual DbSet<Products2> Products2 { get; set; }
        }

    please change it to below

    public class FirstMVC_dbEntities2: DbContext
        {
            public FirstMVC_dbEntities2()
                : base("name=FirstMVC_dbEntities2")
            {
            }
        
            public virtual DbSet<Products2> Products2 { get; set; }
        }

    Tuesday, July 17, 2018 4:53 AM
  • User1941901171 posted

    Hi, I´ve made the change and now this error comes out:

    No connection string named 'FirstMVC_dbEntities2' could be found in the application config file.

    On the web config I have

    <connectionStrings>

    <add name="FirstMVC_connection" connectionString="data source=JHON-PC\SQLEXPRESS;initial catalog=FirstMVC_db;
    integrated security=True;" providerName="System.Data.SqlClient;" />
    </connectionStrings>

    Now, if I change the name of the connection to 

    FirstMVC_dbEntities2 
    Another error asking for ADO appears
    Tuesday, July 17, 2018 6:06 AM
  • User-369506445 posted

    don't worry, for find the problem, please do below step

    you have a model below like

    public class Products2
        {
            public int ID { get; set; }
            public string Description { get; set; }
            public decimal Price { get; set; }
            public System.DateTime LastBuy { get; set; }
            public double Stock { get; set; }
        }

    now change your context to below like :

    public class FirstMVC_dbEntities2: DbContext
        {
            public FirstMVC_dbEntities2()
                : base(@"Data Source=JHON-PC\SQLEXPRESS;Initial Catalog=FirstMVC_db;trusted_connection=true")
            {
            }
        
            public virtual DbSet<Products2> Products2 { get; set; }
        }

    now in your controller

    private FirstMVC_dbEntities2 db = new FirstMVC_dbEntities2();
            public ActionResult Index()
            {
                var model = db.Products2.ToList();
                return View(model);
             
            }

    and put a breakpoint in the highlighted line

    Finally, tell me your feedback.

    note: please exactly use my code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 6:14 AM
  • User1120430333 posted

    A little FYI here.

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    <copied>

    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

    <end>

    https://juristr.com/blog/2012/10/lessions-learned-dont-expose-ef-entities-to-the-client-directly/

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    Tuesday, July 17, 2018 6:39 AM
  • User753101303 posted

    Hi,

    The first one is expected as you are using "FirstMVC_dbEntities2" in your code while the connection string is actually named "FirstMVC_connection".

    So once you fixed that what is the exact error that happens ("asking for ADO" doesn't make think about any particular error, please be more explicit).

    Tuesday, July 17, 2018 8:30 AM
  • User1941901171 posted

    Works fine now, thanks Vahid

    Tuesday, July 17, 2018 10:21 AM