【发布时间】:2016-06-24 15:24:08
【问题描述】:
您好,我有一个实体,我将从数据库中添加两个名为国家和州的表。
这两个表之间存在基于 CountryId 的关系。
我使用“Update Model from database ...”来添加这两种实体类型。
我已经为这两种实体类型手动编写了两个类,如下所示:-
public partial class Country
{
//[Key] //[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int CountryID { get; set; }
public string CountryName { get; set; }
}
public partial class State
{
public int StateID { get; set; }
public string StateName { get; set; }
public int CountryID { get; set; }
}
public DbSet<Country> Countries { get; set; }
public DbSet<State> States { get; set; }
获取国家和状态的控制器:-
public JsonResult GetCountries()
{
List<Country> allCountry = new List<Country>();
using (SunilEntities dc = new SunilEntities())
{
allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
}
return new JsonResult { Data = allCountry, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public JsonResult GetStates(int countryID)
{
List<State> allState = new List<State>();
using (SunilEntities dc = new SunilEntities())
{
allState = dc.States.Where(a => a.CountryID.Equals(countryID)).OrderBy(a => a.StateName).ToList();
}
return new JsonResult { Data = allState, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
但我收到错误消息“实体类型 Country 不是当前上下文模型的一部分”。
在我的控制器中使用这两个表的确切类应该是什么?
在使用更新的表更新实体后,我有什么方法可以获得自动化类?
【问题讨论】:
标签: asp.net-mvc entity-framework