Thursday, 15 July 2010

entity framework - Optional one-to-one relation -



entity framework - Optional one-to-one relation -

when have 2 models this:

public class predictiongroup { [key] public guid predictiongroupid { get; set; } public guid? resultpredictionid { get; set; } [foreignkey("resultpredictionid")] public prediction resultprediction { get; set; } public list<prediction> predictions { get; set; } } public class prediction { [key, databasegenerated(databasegeneratedoption.identity)] public guid predictionid { get; set; } [required] public guid predictiongroupid { get; set; } [foreignkey("predictiongroupid")] public predictiongroup predictiongroup { get; set; } }

this generated:

createtable( "website.predictiongroups", c => new { predictiongroupid = c.guid(nullable: false, identity: true), resultpredictionid = c.guid(), }) .primarykey(t => t.predictiongroupid) .foreignkey("website.predictions", t => t.resultpredictionid) .index(t => t.resultpredictionid); createtable( "website.predictions", c => new { predictionid = c.guid(nullable: false, identity: true), predictiongroupid = c.guid(nullable: false), predictiongroup_predictiongroupid = c.guid(), }) .primarykey(t => t.predictionid) .foreignkey("website.predictiongroups", t => t.predictiongroupid) .foreignkey("website.predictiongroups", t => t.predictiongroup_predictiongroupid) .index(t => t.predictiongroupid) .index(t => t.predictiongroup_predictiongroupid);

when seek come in in database error: unable determine principal end of 'site.data.prediction_predictiongroup' relationship. multiple added entities may have same primary key.

can shine lite on this?

i added fluent api code:

modelbuilder.entity<predictiongroup>() .hasoptional(m => m.resultprediction) .withoptionaldependent() .map(x => x.mapkey("predictionresultgroupid"));

the mapkey optional, hoping been done annotations.

entity-framework ef-code-first code-first-migrations

No comments:

Post a Comment