简体   繁体   English

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

[英]How to Map many one-to-many relationship in ASP.NET MVC?

I have few Domain Models - Address , Customer , Employee , StoreLocation . 我有几个域模型- AddressCustomerEmployeeStoreLocation Address has many to one relationship with Customer and Employee and one to one relationship with StoreLocation. AddressCustomerEmployee具有多对一关系,而与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; }
}

How to Map this relationship?. 如何映射这种关系? I am using ASP.NET MVC 3.0 and Entity Framework 4.1. 我正在使用ASP.NET MVC 3.0和Entity Framework 4.1。

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: 如果您使用的是代码优先(我想您需要这样做,否则,您必须编辑您的Q),第一种方法是下面说明的方法:

Entities: 实体:

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 inherited class: 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);
    }
}

Context Initializer: 上下文初始化器:

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

    }
}

That will create the db-schema below: 这将在下面创建db模式: 一对多关系 Let me know if you have any questions or need clarifications on any part. 让我知道您是否有任何疑问或需要澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net MVC 如何在ApplicationUser和我自己的类之间建立一对多的关系? - ASP.Net MVC How to create a one-to-many relationship between the ApplicationUser and my own class? ASP.NET MVC3和实体框架 - 一个视图中的一对多关系 - ASP.NET MVC3 and Entity Framework- one-to-many relationship in one view 如何在ASP.NET MVC中设置一对多关系? - How to setup one to many relationship in asp.net mvc? 在 ASP.NET MVC 4 中关联 2 个表(一对多) - Relating 2 tables (one-to-many) in ASP.NET MVC 4 ASP.Net实体框架一对多关系:如何建立依赖关系和延迟加载不起作用 - ASP.Net Entity Framework one-to-many relationship: How to establish dependency and lazy loading not working ASP.NET Core中如何将一对多的关系数据添加到数据库中? - How to add one-to-many relationship data to the database in ASP.NET Core? 如何使用ASP.NET MVC 5在EF中实现多对多关系 - How to implement many to many relationship in EF with ASP.NET MVC 5 如何在 ASP.NET CORE MVC 中正确返回嵌套的一对多数据? - How to properly return nested one-to-many data in ASP.NET CORE MVC? ASP.NET MVC中一对多关系ViewModel的CRUD操作 - CRUD Operation for one to many relationship ViewModel in ASP.NET MVC 处理MVC 4 ASP.NET中的“多对一”关系 - Handling a 'Many to One' Relationship in MVC 4 ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM