0

I am using Code First Entity Framework.

I have a Bar table/domain class.

I also have an Address table/domain class. This is because one Bar can have one Address and vice versa.

I have a One-To-One relationship set up between these two entities, with the Bar being the parent. I am now adding a Brewer Entity to my application, which obviously also will need to store an Address.

I want to use the Address table/entity that I already have above to now also represent the Brewer's address. Meaning, A new address can be created when a bar is created or when a new brewer is created. Right now, I have the Address actually using the BarId as its primary and foreign key. Obviously, when I want to add a Brewer record, the Bar won't be part of the transaction, so this will have to be changed.

I'm looking for a way to set up the DB to accept a new Address as long as it is tied to either a Brewer or a Bar but definitely not both at once. The problem with this, is by definition, the BarId and BrewerID that are used inside the Address as the foreign keys must be optional. However in order to set up a One-To-One, you have to either use both [Key][ForeignKey] DataAnnotations Or .HasRequired().WithRequiredParent which is preventing me for doing this. Is there any other way to do this? I'm getting this error:

Bar_Address_Target: : Multiplicity is not valid in Role 'Bar_Address_Target' in relationship 'Bar_Address'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
Brewer_Address_Target: : Multiplicity is not valid in Role 'Brewer_Address_Target' in relationship 'Brewer_Address'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

However, this question is not directly about this error: I do understand the error. The closest most helpful answer on SO is this one. However, it does not address this issue specifically, where I am trying to make multiple one-to-ones but need to have the foreign keys as optional. Without them being optional, the application would require me to add a Bar for every Brewer added and vice versa, which obviously makes no sense.

Community
  • 1
  • 1
the_endian
  • 2,259
  • 1
  • 24
  • 49
  • This isn't possible as it requires the `Bar` table to "know" about the `Brewer` table. Think about how you would set this up in SQL, can you do it there? The only way would be some sort of trigger perhaps. – DavidG Sep 01 '16 at 01:33
  • @DavidG so basically I need to make the Addresses as fields from within the domains huh? Sigh... – the_endian Sep 01 '16 at 01:46
  • Not sure what you mean by that, but can you imagine how you would do this in SQL Server alone? That's my point. One option would be to have the address fields as properties of each entity. You could even mimic a sub-entity with the [`ComplexType`](http://stackoverflow.com/documentation/entity-framework/4161/code-first-dataannotations/18330/complextype-attribute#t=201609010151216336631) attribute. – DavidG Sep 01 '16 at 01:52

0 Answers0