gsharp
gsharp

Reputation: 27927

Configuration of two Entites with EntityTypeConfiguration

I have the two following Entitites with the corresponding EntityTypeConfigurations

public class Master
{
    public int Id { get; set; }
    public int RequestId { get; set; }
    public string ClientNo { get; set; }
    public string SomeValue { get; set; }
    public ICollection<Child> Childs { get; set; }
}

public class MasterConfig : EntityTypeConfiguration<Master>
{
    public MasterConfig()
    {
        ToTable("Master", "MySchema");
        HasKey(k => k.Id);
        HasMany(m => m.Childs)...
       // Connect Master.RequestId to Child.RequestId 
       //     and Master.ClientNo  to Child.ClientNo
    }
}

public class Child
{
    public int Id { get; set; }
    public int RequestId { get; set; }
    public string ClientNo { get; set; }
    public string SomeOtherValue { get; set; }
}

public class ChildConfig : EntityTypeConfiguration<Child>
{
    public ChildConfig()
    {
        ToTable("Child", "MySchema");
        HasKey(k => k.Id);
    }
}

I would like to configure it that way that when I do

myAppContext.Masters.Include(m => m.Childs).First(m => m.Id == 4);

It would load all the Master with ID 4 and the Corresponding maching Childs. Somehow I can't make it work.

Upvotes: 0

Views: 125

Answers (1)

Adil Mammadov
Adil Mammadov

Reputation: 8676

Relationships are based on FK to PK. In your scenario RequestId is not primary key either of Master or Child. You should have Request model, which has RequsetId as PK and should have navigation properties to Master, and Child model should not directly bound to Request, it should bound to Master. Your models should look like this:

public class Request
{
    public int Id { get; set; }
    public string ClientNo { get; set; }

    public virtual ICollection<Master> MasterCollection { get; set; }
}

public class RequestMap  : EntityTypeConfiguration<Request>
{
    HasKey(m => m.Id);
}

public class Master
{
    // Id is same as RequestId
    public int Id { get; set; }
    public int RequestId { get; set; }
    public string SomeValue { get; set; }

    public virtual Request Request { get; set; }
    public virtual ICollection<Child> Childs { get; set; }
}

public class MasterConfig : EntityTypeConfiguration<Master>
{
    public MasterConfig()
    {
        ToTable("Master", "MySchema");
        HasKey(k => k.Id);

        // Map Request and Master
        HasRequired(m => m.Request)
            .WithMany(m => m.MasterCollection)
            .HasForeignKey(m => m.RequestId);
    }
}

public class Child
{
    public int Id { get; set; }
    public string SomeOtherValue { get; set; }
    public int MasterId { get; set; }

    public virtual Master Master { get; set; }
}

public class ChildConfig : EntityTypeConfiguration<Child>
{
    public ChildConfig()
    {
        ToTable("Child", "MySchema");
        HasKey(k => k.Id);

        HasRequired(m => m.Master)
            .WithMany(m => m.Childs)
            .HasForeignKey(m => m.MasterId);
    }
}

By changing your models to suit this, now you can load your Master and related Childs as you want to:

Master master = myAppContext.Masters
    .Include(m => m.Childs)
    .First(m => m.Id == 4);

If you want to load Request data according to Master then:

Master master = myAppContext.Masters
    .Include(m => m.Request)
    .Include(m => m.Childs)
    .First(m => m.Id == 4);

You can also load Master and Request details for any child:

Child child = myAppContext.Childs
    .Include(m => m.Master.Request)
    .First(m => m.Id == 2);

Upvotes: 1

Related Questions