Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello in my sample project ,i have created a model with EF Dbcontext class.But it is not initializing or reflecting in the Controller Class.I am using Asp.net MVC 3 razor ,EF DataFirst to bind database.In my other sample application it works fine.

My Model Code
C#
<blockquote class="FQ"><div class="FQA">Quote:</div>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace InformationManagementSystem.Models
{
    public class Information
    {
        public int SID { get; set; }
        public string FName { get; set; }
        public string MName { get; set; }
        public string LName { get; set; }
        public DateTime DOB { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string DOE { get; set; }
        public string City { get; set; }
        public string Zip { get; set; }
        public string District { get; set; }
        public string Village { get; set; }
        public string Group { get; set; }
        public byte[] image { get; set; }
        public string SPCNum1 { get; set; }
        public string SPCNum2 { get; set; }
        public string SPAddress { get; set; }
        public string SPName { get; set; }
        public string FathName { get; set; }
        public string MothName { get; set; }
        public string Fam1 { get; set; }
        public string Fam2 { get; set; }
        public string Fam3 { get; set; }
        public string Fam4 { get; set; }
        public string Fam5 { get; set; }
        public string Fam6 { get; set; }
        public string Fam7 { get; set; }
        public string Fam8 { get; set; } 
        public string FamCond { get; set; }
        public string FamNotes { get; set; }
        public string Status { get; set; }
        public DateTime EntryDate { get; set; }
        public DateTime DOA { get; set; }

        public string FathOccu { get; set; }
        public string MothOccu { get; set; }



        public class InformationDbContext : DbContext
        {
            public DbSet<Information> Information { get; set; }
        }

        public class InfoDBContext : DbContext
        {

        }

    }

    
}


My Controller Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using InformationManagementSystem.Models;

namespace InformationManagementSystem.Controllers
{
    public class InformationController : Controller
    {
        //
        // GET: /Information/

        InformationDbContext db = new InformationDbContext();


        public ActionResult Index()
        {
            ViewBag.Title = "Arbaneo's Information Management System";
            return View();
        }

        public ActionResult List()
        {
            
            return View();
        }
    }
}


Quote:
Error Code

C#
InformationDbContext db = new InformationDbContext();
Posted
Updated 28-Nov-13 3:56am
v2

1 solution

We have to declare the class outside the model class
Like Below

C#
<blockquote class="FQ"><div class="FQA">Quote:</div>using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Data.Entity;
 
namespace InformationManagementSystem.Models
{
    public class Information
    {
        public int SID { get; set; }
        public string FName { get; set; }
        public string MName { get; set; }
        public string LName { get; set; }
        public DateTime DOB { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string DOE { get; set; }
        public string City { get; set; }
        public string Zip { get; set; }
        public string District { get; set; }
        public string Village { get; set; }
        public string Group { get; set; }
        public byte[] image { get; set; }
        public string SPCNum1 { get; set; }
        public string SPCNum2 { get; set; }
        public string SPAddress { get; set; }
        public string SPName { get; set; }
        public string FathName { get; set; }
        public string MothName { get; set; }
        public string Fam1 { get; set; }
        public string Fam2 { get; set; }
        public string Fam3 { get; set; }
        public string Fam4 { get; set; }
        public string Fam5 { get; set; }
        public string Fam6 { get; set; }
        public string Fam7 { get; set; }
        public string Fam8 { get; set; } 
        public string FamCond { get; set; }
        public string FamNotes { get; set; }
        public string Status { get; set; }
        public DateTime EntryDate { get; set; }
        public DateTime DOA { get; set; }
 
        public string FathOccu { get; set; }
        public string MothOccu { get; set; }
 

 
 
    }

 <blockquote class="FQ"><div class="FQA">Quote:</div>       public class InformationDbContext : DbContext
        {
            public DbSet<information> Information { get; set; }
        }
 
        public class InfoDBContext : DbContext
        {
 
        }</information></blockquote>
 
    
}</blockquote>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900