Entity Framework: Map Foreign Key without Navigation Property?

北城余情 提交于 2019-12-23 17:46:54

问题


Motivation : My EF4.1 DbContext is saving Entities in the wrong order

Reason : Lack of navigation properties on my models

How I want to fix it :

I want to set up foreign key relationships in my DbContext. The catch is that my entity objects have no navigation properties (I'm using it to populate a web service and then firing DTO objects over to my application).

The classes below would be an example. In MinorClass, I want to configure my context so that it knows MajorClassID is a foreign key. The articles I've been finding on the internet on how to explicitly define Foreign Keys involve using navigational properties, which my objects dont have.

Is there a way to map this relationship?

public class MinorClass
{
    public Guid ID {get;set:}
    public Guid MajorClassID {get;set;} // foreign key
    public string Name {get;set;}
}

public class MajorClass
{
    public Guid ID {get;set;}
    public string Name {get;set;}
}

回答1:


Navigation property is primary construct whereas foreign key is helper (imho wrong helper). EF recognizes ordering of DB commands by relationships which are defined by navigation properties. You cannot define relation just by foreign key. You need navigation property on at least one side of the relation.



来源:https://stackoverflow.com/questions/8452725/entity-framework-map-foreign-key-without-navigation-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!