0

I have a (simplified) entity model as follows

public class Address {
}

public class PersonAddress {
  public Address Address {get; set;}
}

public class Person {
  ICollection<PersonAddress> Addresses { get; set;}

  public void SetAddresses(params Address addresses[]) {
    Addresses.Clear();
    addresses.Select(a => new PersonAddress(this, a)).ForEach(Addresses.Add);
  }
}

This is implemented as a join-class on purpose as it actually makes more sense in my business model than a many-to-many.

Understandably, when SetAddresses is executed what happens in the database is that the PERSON_ADDRESS.FK_PERSON_ID reference in the now removed PERSON_ADDRESS record gets nulled out. What I want however is for it to be deleted entirely.

Edit: From the comments below I understand that what I want is maybe for PersonAddress to have an "Identifying Relationship" from Person but how do I configure this given the above model using the fluent api?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • See http://stackoverflow.com/a/11033988 – haim770 Jun 04 '13 at 19:51
  • @haim770 So I would *have* to have a composite key? Wouldn't that end up having performance implications? – George Mauer Jun 04 '13 at 20:06
  • Frankly, i don't know how it would affect your performance. i was just curious as well. – haim770 Jun 04 '13 at 20:11
  • @haim770 ok so I've tried `modelBuilder.Entity().HasKey(pa=>new{pa.PersonAddressId, pa.Person.PersonId})` but this results in an error that `The properties expression ' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }'` Note that the latter is exactly what I was doing just I have an actual reference, not a key one. – George Mauer Jun 04 '13 at 20:22
  • Your `composite key` should be a composition of `PersonId` and `AddressId`, not `PersonAddressId` (which shouldn't exist at all). – haim770 Jun 04 '13 at 20:29
  • Oh I see...but I DO need the references to the actual classes, not just their ids – George Mauer Jun 04 '13 at 20:29
  • You can have them as well – haim770 Jun 04 '13 at 20:32
  • So I'm not sure how I would do the mapping for the foreign key then - how would it know that `PersonId` and `AddressId` fields map to Person or Address columns? – George Mauer Jun 04 '13 at 21:19
  • See http://pastebin.com/eATm1GEH and read http://www.ladislavmrnka.com/2011/05/foreign-key-vs-independent-associations-in-ef-4/ – haim770 Jun 05 '13 at 06:05
  • @haim770 So I'm still not exactly sure how to code this - would I have to assign `AddressId` and `PersonId` manually? How would it know to fill them otherwise? If using database generated ids, wouldn't that mean that I couldn't create a `PersonAddress` for a newly created `Person`? – George Mauer Jun 05 '13 at 15:49
  • You don't need to assign them manually. Entity Framework will fill them for you once you're manually mapping them to the `Foreign Key` in the table, see my revised code here: http://pastebin.com/Rq09drfF – haim770 Jun 05 '13 at 16:27
  • I'm going to have to come back to this later - actually trying to implement it I got errors and apparently the auto-generated rollback migration was invalid so I've got some fixing to do... – George Mauer Jun 05 '13 at 18:23

0 Answers0