2011-10-10 139 views
0

我有幾個域模型 - Address,Customer, Employee, StoreLocationAddressCustomerEmployee有多對一的關係,並且與StoreLocation具有一對一的關係。如何映射ASP.NET MVC中的許多一對多關係?

public class Address 
{ 
    public int Id; 
    public string Line1 { get; set; } 
    public string Line2 { get; set; } 
    public string Line3 { get; set; } 
} 

public class Customer 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public IList<Address> Addresses { get; set; } 
} 

public class StoreLocation 
{ 
    public int Id { get; set; } 
    public string ShortCode { get; set; } 
    public string Description { get; set; } 
    public Address Address { get; set; } 
} 


public class Employee 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public DateTime Dob { get; set; } 
    public IList<Address> Addresses { get; set; } 
} 

如何映射這種關係?我正在使用ASP.NET MVC 3.0和實體框架4.1。

回答

13

如果使用代碼優先(我想你想這一點,否則,您必須修改你的Q),第一種方式是這樣的解釋如下:

實體:

public class Address { 
    [Key] 
    public int Id { get; set; } 
    public string Line1 { get; set; } 
    public string Line2 { get; set; } 
    public string Line3 { get; set; } 

    public virtual Customer Customer { get; set; } 
    public virtual StoreLocation StoreLocation { get; set; } 
    public virtual Employee Employee { get; set; } 

    public int? CustomerId { get; set; } 

    public int? EmployeeId { get; set; } 
} 

public class Customer { 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<Address> Addresses { get; set; } 
} 

public class StoreLocation { 
    [Key] 
    public int Id { get; set; } 
    public string ShortCode { get; set; } 
    public string Description { get; set; } 
    public virtual Address Address { get; set; } 
} 


public class Employee { 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public DateTime Dob { get; set; } 
    public virtual ICollection<Address> Addresses { get; set; } 
} 

的DbContext繼承的類:

public class ManyOneToManyContext : DbContext { 

    static ManyOneToManyContext() { 
     Database.SetInitializer<ManyOneToManyContext>(new ManyOneToManyInitializer()); 
    } 

    public DbSet<Address> Addresses { get; set; } 
    public DbSet<Customer> Customers { get; set; } 
    public DbSet<StoreLocation> StoreLocations { get; set; } 
    public DbSet<Employee> Employees { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) { 

     modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 

     modelBuilder.Entity<Customer>().HasMany(c => c.Addresses).WithOptional(a => a.Customer).HasForeignKey(a => a.CustomerId); 

     modelBuilder.Entity<StoreLocation>().HasRequired(s => s.Address).WithOptional(a => a.StoreLocation).Map(t => t.MapKey("AddressId")); 

     modelBuilder.Entity<Employee>().HasMany(e => e.Addresses).WithOptional(a => a.Employee).HasForeignKey(e => e.EmployeeId); 
    } 
} 

背景信息在itializer:

public class ManyOneToManyInitializer : DropCreateDatabaseAlways<ManyOneToManyContext> { 
    protected override void Seed(ManyOneToManyContext context) { 

    } 
} 

,將創建下面的DB-模式: Many one-to-many relationship 讓我知道如果您有任何疑問或需要任何部分澄清。

+0

HI Javad_Amiry,謝謝你的回答。當我遇到一些疑問時,我總是來到Stackoverflow,因爲有很多人像你一樣幫助其他人。謝謝。還有一個問題是,如果我添加更多的實體引用多對一的地址,將會有許多字段,如customerid,employeeid添加到地址表中有很多空值,是一個正確的數據庫設計? – RAM

+0

嗨,不客氣。不,我認爲這不可能是一種正常和良好的設計。我認爲你必須使用第三表來分配實體。 **或者**如果實體數量很多,您可以將每個實體的「地址」合併。但是,這個問題與你的主要問題是分開的,你應該問一個分開的問題(因爲SOF的問題)。另外,如果答案是,幫助你解決問題,請接受它。謝謝。 –