0

I have an Entity Framework 6.1.3 Code First data access library with C# and .NET Framework 4.7.

I have these two tables:

public class AggregationChildren
{
    public int AggregationChildrenId { get; set; }
    public int AggregationId { get; set; }
    public int Position { get; set; }

    public virtual Aggregation Aggregation { get; set; }
    public virtual Code Code { get; set; }
}

public class Code
{
    public int CodeId { get; set; }

    public string Serial { get; set; }
    [ ... ]

    public virtual AggregationChildren AggregationChild { get; set; }
    [ ... ]
}

And their configurations files:

class AggregationChildrenConfiguration : EntityTypeConfiguration<AggregationChildren>
{
    public AggregationChildrenConfiguration()
    {
        HasKey(ag_ch => ag_ch.AggregationChildrenId);

        HasRequired(ag_ch => ag_ch.Aggregation)
            .WithMany(ag => ag.AggregationChildren)
            .HasForeignKey(ag_ch => ag_ch.AggregationId);

        HasRequired(ag_ch => ag_ch.Code)
            .WithOptional(c => c.AggregationChild)
            .WillCascadeOnDelete(false);
    }
}
class CodeConfiguration : EntityTypeConfiguration<Code>
{
    public CodeConfiguration()
    {
        HasKey(c => c.CodeId);

        [ ... ]
    }
}

I'm trying to set here that if there data in AggregationChildren there must be the same data (AggregationChildrenId == CodeId) in Code table, but the data in CodeId could not be in AggregationChildren table.

My question is: Is it need it the following configuration in AggregationChildrenConfiguration class?

HasRequired(ag_ch => ag_ch.Code)
    .WithOptional(c => c.AggregationChild)
    .WillCascadeOnDelete(false);
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • Yes, it's needed - [here](https://weblogs.asp.net/manavi/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations) is explanation why. If you are wondering how to port it to EFC2.0, take a look at [Relationships](https://learn.microsoft.com/en-us/ef/core/modeling/relationships), especially the **One-to-one** section. – Ivan Stoev Sep 27 '17 at 08:08
  • @IvanStoev I've asked this question to know how to set a **One-to-zero** relationship with Entity Framework Core 2.0: https://stackoverflow.com/questions/46441029/from-entity-framework-6-1-3-to-entity-framework-core-2-0 – VansFannel Sep 27 '17 at 08:50
  • Yeah, I thought you have deleted it :) – Ivan Stoev Sep 27 '17 at 08:53
  • @IvanStoev I have update it and undelete it. Thanks. – VansFannel Sep 27 '17 at 08:55

0 Answers0