c# - Entity framwork mutiple object relation -
i have table 2 foreignkeys same parent table, edge.startstationid , edge.endstationid.
i trying map theese objets in entity framework, can't find workaround seems fix problem. i've found solutions using 2 collections on parent (station), i'm not interested in.
station (parent) class:
public partial class station { public station() { this.reservations = new list<reservation>(); this.stationmaintenances = new list<stationmaintenance>(); } public int id { get; set; } public int typeid { get; set; } public string title { get; set; } public string description { get; set; } public decimal stationlat { get; set; } public decimal stationlong { get; set; } public bool isoperational { get; set; } public bool isactive { get; set; } public datetime createddate { get; set; } public virtual batterystorage batterystorages { get; set; } public virtual list<reservation> reservations { get; set; } public virtual list<stationmaintenance> stationmaintenances { get; set; } public virtual list<edge> edges { get; set; } public virtual stationtype stationtype { get; set; } } edge (child) class:
public partial class edge { public int id { get; set; } public int startstationid { get; set; } public virtual station startstation { get; set; } public int endstationid { get; set; } public virtual station endstation { get; set; } public decimal distance { get; set; } public decimal time { get; set; } public bool isactive { get; set; } } the edge map class, added in onmodelcreating.
public edgemap() { // primary key this.haskey(t => t.id); // properties // table & column mappings this.totable("edges"); this.property(t => t.id).hascolumnname("id"); this.property(t => t.startstationid).hascolumnname("startstationid"); this.property(t => t.endstationid).hascolumnname("endstationid"); this.property(t => t.distance).hascolumnname("distance"); this.property(t => t.time).hascolumnname("time"); this.property(t => t.isactive).hascolumnname("isactive"); // relationships //this.hasoptional(t => t.startstation) // .withmany(t => t.edges) // .hasforeignkey(d => d.starstationid); //this.hasoptional(t => t.endstation) // .withmany(t => t.edges) // .hasforeignkey(d => d.endstationid); } exception:
one or more validation errors detected during model generation: system.data.entity.edm.edmassociationtype: : multiplicity conflicts referential constraint in role 'edge_endstation_target' in relationship 'edge_endstation'. because of properties in dependent role non-nullable, multiplicity of principal role must '1'.
i think particular error posted complaining hasoptional. since foreign keys (startstationid , endstationid) non-nullable, it's expecting required mapping. try changing hasrequired, or change type of startstationid , endstationid int? knows navigation properties can null.
as edges collection, should contain? edge references station either startstationid or endstationid values? if so, don't think can single collection.
Comments
Post a Comment