2011-10-10 135 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的问题)。另外,如果答案是,帮助你解决问题,请接受它。谢谢。 –