Question

Bit of trouble trying to understand and configure a few relationships. Here are my requirements:

I have a one-to-one relationship, where the principle entity has a navigation property defined in its class, but the dependent does not. Neither entity can exist without the other - how do I specify this relationship in an EntityConfiguration file?

Here are the classes: EDIT (removed PropertyId from PropertyDetail - Property is now the Dependent)

public abstract class EntityBase
{
   public int Id { get; set; }
}

public class PropertyDetail : EntityBase
{
}

public class Property : EntityBase
{
  public int PropertyDetail { get; set; }

  public PropertyDetail PropertyDetail { get; set; }
}

Is it more, or does Entity Framework require a Navigation property on both ends for a one-to-one relationship where both are required?

Furthermore, how do I properly specify a one-to-many relationship where the principle has no knowledge of the relationship - meanwhile, the dependent entity contains a collection of the principle? Relationship is Optional on both ends.

public class Plaintiff : EntityBase
{
}

public class PlaintiffAttorney: EntityBase
{
  public List<Plaintiff> PlaintiffList { get; set; }
}

Again, configurations are done within EntityConfiguration files. Any help is greatly appreciated. Thanks in advance. :-)

Was it helpful?

Solution

To create a one-to-one relationship, go to the EntityConfiguration class of the principal entity and write this:

HasRequired(p => p.PropertyDetail).WithRequiredDependent();

As for the one-to-many relationship, you can go to the dependent entity's config class and do this:

HasOptional(d => d.PlaintiffList).WithMany();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top