使用数据库更新现有实体框架(通过从db添加新表)

时间:2016-06-23 11:58:49

标签: asp.net-mvc entity-framework

您好我有一个实体,我将从数据库中添加两个名为country and state的表。

这两个表之间存在基于CountryId的关系。

我使用了数据库中的"更新模型......"添加这两种实体类型。

enter image description here

我为这两种实体类型手动编写了两个类,如下所示: -

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 };
       }

但是我收到错误&#34;实体类型Country不是当前上下文模型的一部分&#34;。

在我的控制器中使用这两个表的确切类应该是什么?

在使用较新的表更新实体后,有什么方法可以获得自动化类吗?

2 个答案:

答案 0 :(得分:0)

在yourmodel.edmx文件下有yourmodel.tt,它会生成相关的类,因此不需要编写这些类。通过使用相关的命名空间,您可以使用它们。

enter image description here

答案 1 :(得分:0)

我得到了解决方案更改模型如下: -

 public partial class Country
    {
        public Country()
        {
            this.States = new HashSet<State>();
        }

        public int CountryID { get; set; }
        public string CountryName { get; set; }

        public virtual ICollection<State> States { get; set; }
    }

  public partial class State
    {
        public int StateID { get; set; }
        public string StateName { get; set; }
        public Nullable<int> CountryID { get; set; }

        public virtual Country Country { get; set; }
        public virtual State State1 { get; set; }
        public virtual State State2 { get; set; }
    }

并按以下方式更改控制器: -

   public JsonResult GetCountries()
            {              
                using (SunilEntities dc = new SunilEntities())
                {
                    var ret = dc.Countries.Select(x => new { x.CountryID, x.CountryName }).ToList();
                    return Json(ret, JsonRequestBehavior.AllowGet); 
                }
            }
            // Fetch State by Country ID
            public JsonResult GetStates(int countryID)
            {           
                using (SunilEntities dc = new SunilEntities())
                {                    
                    var ret = dc.States.Where(x => x.CountryID == countryID).Select(x => new { x.StateID, x.StateName }).ToList();
                    return Json(ret, JsonRequestBehavior.AllowGet);
                }              
            }