2

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
    } 
tereško
  • 58,060
  • 25
  • 98
  • 150
  • possible duplicate of [EF Code First Fluent API specifying the Foreign Key property](http://stackoverflow.com/questions/19359608/ef-code-first-fluent-api-specifying-the-foreign-key-property) – Marco Sep 26 '14 at 08:29

1 Answers1

0

You can make a configuration class for each of your entity classes:

internal partial class CountryConfiguration : EntityTypeConfiguration<Country>
    {
        public CountryConfiguration()
        {
            ToTable("dbo.Country");
            HasKey(x => x.Country_Id);

            Property(x => x.Country_Id).HasColumnName("Country_Id").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.Country_Name).HasColumnName("Country_Name").IsOptional().HasMaxLength(255);
        }
}


internal partial class StateConfiguration : EntityTypeConfiguration<State>
{
        public StateConfiguration()
        {
            ToTable("dbo.State");
            HasKey(x => x.State_Id);

            Property(x => x.State_Id).HasColumnName("State_Id").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.State_Name).HasColumnName("State_Name").IsOptional().HasMaxLength(255);
            // Foreign keys
            HasOptional(a => a.Country).WithMany(b => b.States).HasForeignKey(c => c.CountryId); 
        }
}

And then in your OnModelCreating method, you can wire them up like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CountryConfiguration());
        modelBuilder.Configurations.Add(new StateConfiguration());
    } 

If you already have an existing database, there is a very handy tool where can generate code-first POCO classes from the database.

tranceporter
  • 2,241
  • 1
  • 21
  • 23