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

发布于 2024-12-08 21:17:41 字数 992 浏览 0 评论 0原文

我有几个域模型 - 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。

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; }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2024-12-15 21:17:41

如果您使用代码优先(我认为您想要这个,否则,您必须编辑您的 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继承类:

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) {

    }
}

这将创建以下数据库模式:
许多一对多关系
如果您对任何部分有任何疑问或需要澄清,请告诉我。

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:

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:

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:
Many one-to-many relationship
Let me know if you have any questions or need clarifications on any part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文