【问题标题】:Update existing entity framework with database (by adding new tables from db)使用数据库更新现有实体框架(通过从 db 添加新表)
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 当您在其中添加新表时,它是否适用于现有实体?
    • 是的,你可以试试
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2019-12-17
      • 2014-09-04
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多