如何在ASP.NET MVC中映射许多一对多关系?

时间:2011-10-10 22:40:15

标签: c# entity-framework ef-code-first one-to-many

我的域模型很少 - AddressCustomerEmployeeStoreLocationAddressCustomerEmployee有多对一的关系,与StoreLocation有一对一的关系。

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和Entity Framework 4.1。

1 个答案:

答案 0 :(得分: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);
    }
}

上下文初始化程序:

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

    }
}

这将在下面创建db-schema: Many one-to-many relationship 如果您对任何部分有任何疑问或需要澄清,请与我们联系。