I an using Mvc (Country -> State -> City ) and wants to make Foreign-Key Relationship using Fluent Api then here is the model.
public class Country
{
public int Country_Id { get; set; }
public String Country_Name { get; set; }
}
public class State
{
public string State_Name { get; set; }
public int State_Id { get; set; }
public virtual Country Country { get; set; }
}
public class City
{
public string City_Name { get; set; }
public int City_Id { get; set; }
public virtual State State { get; set; }
}
Now I make UserContext class to define the DbContext and Fluent api can anyone nows how to make relation between these Entities
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection") {}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Country> TbCountries { get; set; }
public DbSet<State> TbState { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Country
modelBuilder.Entity<Country>().HasKey(c => c.Country_Id);
modelBuilder.Entity<Country>().Property(p => p.Country_Name).HasColumnType("VARCHAR").IsRequired().HasMaxLength(50);
//State
}