Dbcontext类没有反映在控制器中 [英] Dbcontext Class is not reflecting in controller
本文介绍了Dbcontext类没有反映在控制器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,在我的示例项目中,我创建了一个带有EF Dbcontext类的模型。但它没有初始化或反映在Controller类中。我正在使用Asp.net MVC 3 razor,EF DataFirst来绑定数据库。在我的其他示例应用程序工作正常。
我的模型代码
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
<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
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:
错误代码
InformationDbContext db = new InformationDbContext();
推荐答案
我们必须在模型类之外声明类
喜欢以下
We have to declare the class outside the model class
Like Below
<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>
这篇关于Dbcontext类没有反映在控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文