Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i have an entity and i am gonna add two tables named as country and state.
There is a relation between these two tables based on CountryId.
I used the "Update Model from database ..." to add these two entity types.
But I getting an error "The entity type Country is not part of the model for the current context.".

I have manually written two classes for these two entity-types given as below:-

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


and
C#
public DbSet<Country> Countries { get; set; }
public DbSet<State> States { get; set; }


Controller to fetch coutries and states :-

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


What should be the exact classes to be used to use these two tables in my controller?
Is there any way I can get automated classes after entity being updated with newer tables?

What I have tried:

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

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