2
나는 Mvc (Country -> State -> City)를 사용하며 Fluent API를 사용하여 외래 키 관계를 만들고 싶다. 여기에 모델이있다.Mvc, 외래 기업과의 관계
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; }
}
는 지금은 정보 UserContext 클래스 사람이 당신은 당신의 엔티티 클래스의 각 구성 클래스를 만들 수 있습니다
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
}
[EF 코드의 첫 번째 Fluent API는 외래 키 속성을 지정합니다] (http://stackoverflow.com/questions/19359608/ef-code-first-fluent-api-specifying-the-foreign-key-property) – Marco