4

I have the following configuration. I have a User. The user has a postal Address and a Physical Address (see below).

public class UserProfile
{
    public Guid UserProfileId {get; set;}

    public Guid PostalAddressId {get;set;}
    public virtual Address PostalAddress {get;set;}

    public Guid PhysicalAddressId {get;set;}
    public virtual Address PhysicalAddress {get;set;}
}

public class Address{
    public Guid AddressId {get;set;}
    public string LineOne {get;set;}
    public string LineTwo {get;set;}
    public string LineThree {get;set;}
}

Next is my fluent mapping

public UserProfileMapping()
{
    ToTable("UserProfile");
    HasKey(pk => pk.UserProfileId);
    Property(pr => pr.UserProfileId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    HasRequired(a => a.PostalAddress).WithMany().HasForeignKey(fk => fk.PostalAddressId);
    HasRequired(a => a.PhysicalAddress).WithMany().HasForeignKey(fk => fk.PhysicalAddressId);
}

public AddressMapping()
{
    ToTable("Address");
    HasKey(pk => pk.AddressId);
    Property(pr => pr.AddressId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}

When i try to run update-database i get:

Introducing FOREIGN KEY constraint 'FK_dbo.UserProfile_dbo.Address_PhysicalAddressId' on table 'UserProfile' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I am required to not have any reverse mapping on the Address object. I also need to be able to hard delete the UserProfile object including its children addresses (of which there will only be two of in the address table).

My question is, Is what I want possible? If so, what am I doing wrong?

[EDIT] The Address object is also used in objects such as UserCompany and Customer.

Skbenga
  • 105
  • 1
  • 7

1 Answers1

0

Because the addresses are not UserProfile's children, but just association, you don't want to use cascade deletion :

HasRequired(a => a.PostalAdress)
    .WithMany()
    .HasForeignKey(fk => fk.PostalAddressId)
    .WillCascadeDelete(false);
Kinetic
  • 2,640
  • 17
  • 35
  • Sorry I didn't give enough information. The Address object can belong to more than just a UserProfile. a Company Object can also have an address and a Customer can also have an address. from my understanding this would not work using your suggested answer because the one to one mapping. i can update my question to reflect this. – Skbenga Nov 21 '16 at 13:28
  • How can you delete the userProfile including its children addresses if the adresses can be linked to other object? – Kinetic Nov 21 '16 at 14:42
  • You don't want cascade deletion if the adress is not owned by the UserProfile. – Kinetic Nov 21 '16 at 14:47
  • So an address will either belong to a customer, or a User or a Company. After giving this some thought I think I designed the tables wrong. Originally I was going to have UserPhysicalAddress, UserPostalAddress, CompanyAddress and CustomerAddress, but because they all had the same model structure I decided to make a single Address model. Implementing this has become a headache. – Skbenga Nov 22 '16 at 07:40