Friday, 15 May 2015

c# - Entity Framework --Unable to determine the principal end of an association between the types? -



c# - Entity Framework --Unable to determine the principal end of an association between the types? -

i creating entities using code first schema when run application generating exception

unable determine principal end of association between types 'webapplication1.models.dateofproject' , 'webapplication1.models.projects'. principal end of association must explicitly configured using either relationship fluent api or info annotations.

my scenario implement 1.1 relation between projects , dateofprojects such 1 project has 1 dateofproject.

my code is

public class projects { [key()] [databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.identity)] public int projectid { get; set; } public string projecttitle { get; set; } public string projectdescriptions { get; set; } public dateofproject dateofproject { get; set; } public virtual icollection<applicationuser> applicationuser { get; set; } public virtual icollection<tasksheetmanagement> tasksheetmanagement { get; set; } } public class dateofproject { public int dateofprojectid { get; set; } [foreignkey("projectid")] public projects projects { get; set; } public datetime dateofprojectcreation { get; set; } public nullable<datetime> expectedcompletiondate { get; set; } public nullable<datetime> projectcompletiondate { get; set; } }

and within dbcontextclass inonmodelcreating function

modelbuilder.entity<projects>().haskey(pk => pk.projectid).totable("projects"); modelbuilder.entity<dateofproject>().haskey(pk => pk.dateofprojectid).totable("dateofproject"); modelbuilder.entity<projects>().hasrequired(p => p.dateofproject).withrequiredprincipal(c => c.projects);

i not resolve problem.

if want 1 : 1 relationship have remove [foreignkey("projectid")] attribute. 1: 1 relationships primary key used. if want separate foreign key column 1 : * relationship.

c# asp.net entity-framework linq-to-entities entity-framework-5

No comments:

Post a Comment