ナビゲーションは、住所、電話、人から機能しています。People に、住所または電話へのナビゲーション プロパティを取得させることができません。people の私のナビゲーション プロパティは、電話番号と住所に対して null を表示します。
お知らせ下さい。EF 4.5 の使用
public class Person
{
[Key]
public int PersonID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string FullName
{
get
{
return string.Format("{0} {1}", this.FirstName, this.LastName);
}
}
public string Email { get; set; }
public virtual ICollection<Address> Address { get; set; }
public virtual ICollection<Phone> Phone { get; set; }
}
public class Phone
{
[Key]
public int PhoneID { get; set; }
public int PersonID { get; set; }
public virtual Person Person { get; set; }
[Required]
public string PhoneNum { get; set; }
[Required]
public int PhoneNumTypeID { get; set; }
}
public class Address
{
[Key]
public int AddressID { get; set; }
public int PersonID { get; set; }
public virtual Person Person { get; set; }
[Required]
public string Street { get; set; }
public int AddressTypeID { get; set; }
public virtual AddressType AddressType { get; set; }
}
public class AddressType
{
[Key]
public int AddressTypeID { get; set; }
[Required]
public string AddressTypeDesc { get; set; }
}
public class ContactContext : DbContext
{
public ContactContext() : base("Contact") { }
public DbSet<Person> People { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Phone> Phones { get; set; }
public DbSet<AddressType> AddressTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Address>()
.HasRequired(x => x.Person)
.WithMany()
.HasForeignKey(x => x.PersonID);
modelBuilder.Entity<Phone>()
.HasRequired(x => x.Person)
.WithMany()
.HasForeignKey(x => x.PersonID);
modelBuilder.Entity<Address>()
.HasRequired(x => x.AddressType)
.WithMany()
.HasForeignKey(x => x.AddressTypeID);
base.OnModelCreating(modelBuilder);
}
}