Как отобразить многие отношения один-ко-многим в ASP.NET MVC? - PullRequest
0 голосов
/ 11 октября 2011

У меня есть несколько моделей доменов - Address, Customer, Employee, StoreLocation.Address имеет отношение один к одному с Customer и Employee и отношение один к одному с 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 Ответ

13 голосов
/ 11 октября 2011

Если вы используете код вначале (я думаю, что вы хотите этого, иначе вы должны отредактировать свой 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 ниже: Many one-to-many relationship Дайте мне знать, если у вас есть какие-либо вопросы или вам нужны разъяснения по любой части.

...