i have simple schema, simplified further question
table: airport iata char(3) primary key name varchar(20) table: airport_terminal iata char(3) (comp key1) terminalname (comp key2) pocos public sealed class airport { [key] public string iata { get; set; } public string name { get; set; } public icollection<airportterminal> terminals { get; set; } } public class airportterminal { [key, column(order = 0)] public string iata { get; set; } [key, column(order = 1)] public string terminal { get; set; } public airport airport { get; set; } }
airportterminal configuration
modelbuilder.entity<airportterminal>() .hasoptional<airport>(s => s.airport) .withmany(s => s.terminals) .hasforeignkey(s => s.iata);
some airports (in application) have multiple terminals , not. want react terminals property when there terminals defined given airport. if enter single record every airport, configuration works. when attempt lookup airport, get:
"one or more validation errors detected during model generation: multiplicity conflicts referential constraint in role 'airportterminal_airport_target' in relationship 'airportterminal_airport'. because of properties in dependent role non-nullable, multiplicity of principal role must '1'."
research shows error occurs when non-nullable property exists on optional member, cannot set null. airportterminal contains 2 strings, nullable.
any ideas?
consider using key tables. best instead of multiple key. place index constraint ensure uniqueness in properties iata
, terminal
.
pocos:
public sealed class airport { [key] public guid id { get; set; } public string iata { get; set; } public string name { get; set; } public icollection<airportterminal> terminals { get; set; } } public class airportterminal { [key] public guid id { get; set; } public string iata { get; set; } public string terminal { get; set; } public airport airport { get; set; } public guid? airportid { get; set; } }
configuration:
modelbuilder.entity<airportterminal>() .hasoptional<airport>(s => s.airport) .withmany(s => s.terminals) .hasforeignkey(s => s.airportid);
Comments
Post a Comment