如何映射许多ASP.NET MVC一个一对多的关系? [英] How to Map many one-to-many relationship in ASP.NET MVC?

查看:2080
本文介绍了如何映射许多ASP.NET MVC一个一对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个领域模型 - 地址客户员工 StoreLocation 地址客户员工和多对一的关系。人跟StoreLocation一一对应关系。

I have few Domain Models - Address, Customer, Employee, StoreLocation. Address has many to one relationship with Customerand Employee and one to one relationship with 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。

How to Map this relationship?. I am using ASP.NET MVC 3.0 and Entity Framework 4.1.

推荐答案

如果您正在使用代码优先(我想你想这一点,否则,你必须编辑你的Q),第一种方式是这样的解释如下:

If you are using code-first (I think you want this, else, you have to edit your Q), the first way is the way explained below:

实体:

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-模式:

让我知道如果您有任何疑问或需要任何部分澄清。

That will create the db-schema below: Let me know if you have any questions or need clarifications on any part.

这篇关于如何映射许多ASP.NET MVC一个一对多的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆