简体   繁体   中英

Map Entity Relationships using Fluent APIs - Entity Framework

I have Following Entities in Application.

Student
Parent (This Parent can be Father/Guardian)
Address

They have following classes defined.

public Class Student
{
    public int StudentId {get; set;}

    //Addresses
    public Address TemporaryAddress {get;set;}
    public Address PermanentAddress {get;set;}

    //Parent
    public Parent Father {get;set;}
    public Parent Guardian {get;set;}
}

public class Parent
{
    public int ParentId {get;set;}
    public string Name {get;set;}

    //Addresses
    public Address TemporaryAddress {get;set;}
    public Address PermanentAddress {get;set;}

}

public class Address
{
    public int AddressId {get;set;}
    public string Country  {get;set;}
    public string State  {get;set;}
    public string Town  {get;set;}
    public string House#  {get;set;}
}

And i want to have following sort of relationship between them

Student
        ->TemporaryAddress (Address)
        ->PermanentAddress (Address)
        ->Father           (Parent)
                ->TemporaryAddress (Address)
                ->PermanentAddress (Address)
        ->Guardian         (Parent)
                ->TemporaryAddress (Address)
                ->PermanentAddress (Address)

In other words,

Student has one TemporaryAddress and one PermanentAddress . Student has one Father and one Guardian (They both can be same ie a Guardian can be Father as well) Parent (Father/Guardian) has TemporaryAddress and PermanentAddress.

How can i acheive this using Fluent APIs of Entity Framework ? What other fields needs to be added in Address and Parent Entities to make this relation possible.

Thanks.

Using the classes you have provided with Entity Framework creates the necessary tables and relations and from your question it is not entirely clear what problems you are facing. However, I assume that you want Parent and Address relations to be non-optional as opposed to the default relations created by Entity Framework that are optional.

You can use the fluent API to specify that a relationship is required. Here it is specified that a Student requires a non-null value for the Father property:

modelBuilder
  .Entity<Student>()
  .HasRequired(p => p.Father);

Entity framework will by default add cascading delete to a foreign key relationship so deleting say a Parent row will delete the associated Address rows. However, as you can reuse Parent and Address rows you have to turn this off. This can be done for the entire DbContext by removing the OneToManyCascadeDeleteConvention .

Putting this together you can create a DbContext using the fluent API:

public class Context : DbContext {

  public IDbSet<Student> Students { get; set; }

  public IDbSet<Parent> Parents { get; set; }

  public IDbSet<Address> Addresses { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    modelBuilder
      .Entity<Student>()
      .HasRequired(p => p.Father);
    modelBuilder
      .Entity<Student>()
      .HasRequired(p => p.Guardian);
    modelBuilder
      .Entity<Student>()
      .HasRequired(p => p.PermanentAddress);
    modelBuilder
      .Entity<Student>()
      .HasRequired(p => p.TemporaryAddress);
    modelBuilder
      .Entity<Parent>()
      .HasRequired(p => p.PermanentAddress);
    modelBuilder
      .Entity<Parent>()
      .HasRequired(p => p.TemporaryAddress);
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM