Relationship 1:1 with abstract class in EF

Asked

Viewed 118 times

0

All right, guys?

I have the following problem: I have two classes Person and Property who may have an address (Address), but the class Person may have 0:N Address and the Property 0:1. Knowing that I transformed the class Address in abstract and created two other classes that inherit her attributes, as print below.

Modelagem

After this modeling, I created the classes Address, Addressperson and Addressproperty as follows:

[Table("ADDRESSES")]
public abstract class Address
{
    [Key]
    public int AddressId { get; set; }

    [MaxLength(200)]
    public string Street { get; set; }

    [MaxLength(10)]
    public string ZipCode { get; set; }

    [MaxLength(6)]
    public string Number { get; set; }

    [MaxLength(100)]
    public string Neighborhood { get; set; }

    [MaxLength(100)]
    public string City { get; set; }

    [MaxLength(100)]
    public string State { get; set; }

    [MaxLength(100)]
    public string Complement { get; set; }

    public StatusAddressEnum.StatusAddress Status { get; set; }
    public KindAddressEnum.KindAddress KindAddress { get; set; }
}

[Table("ADDRESSES_PERSONS")]
public class AddressPerson : Address
{
    public AddressPerson()
    {
        KindAddress = KindAddressEnum.KindAddress.PERSON;
    }

    [ForeignKey("Person")]
    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}

[Table("ADDRESS_PROPERTIES")]
public class AddressProperty : Address
{
    public AddressProperty()
    {
        KindAddress = KindAddressEnum.KindAddress.PROPERTY;
    }

    [Key, ForeignKey("Property")]
    public int PropertyId { get; set; }
    public virtual Property Property { get; set; }
}

When trying to create a Migration I have the following error: Addressproperty_property_source: : Multiplicity is not Valid in Role 'Addressproperty_property_source' in Relationship 'Addressproperty_property'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be ''.*

If I remove the attribute Addressid class Address and move on to the Addressperson, the RU claims that the class Address does not possess a Key. What would be the right way to solve this problem?

  • I think the relationship is too many for many! ??? kind Person and Address generates a table PersonAddress in the same logic with Property, good at least I think ... !!!

  • @Virgilionovic is correct the modeling, an address(Address) belongs to only one person (Person) or a property(Property).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.