【发布时间】:2014-09-26 07:44:00
【问题描述】:
我使用 Mvc (Country -> State -> City) 并想使用 Fluent Api 建立外键关系,那么这里是模型。
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 类来定义 DbContext 和 Fluent api 谁能知道如何在这些实体之间建立关系
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
}
【问题讨论】:
标签: c# asp.net-mvc ef-fluent-api