开发者

EF Code First using non-Primary key to create one-to-many relationship

I have the following two entities:

public class User
{
    public int PKID { get; set; }
    public string Login { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public virtual ICollection<AppAccess> AppAccessList { get; set; }
}

public class AppAccess
{
    public int PKID {get; set; }
    public string Login { get; set; }
    public string AppCode { get; set; }
}

The PKID field is the primary key and identity column of each table but the Login field is what links them in a one-to-many relationship where the User is the principal entity.

How can I setup the navigation property AppAccessList using the fluent API, if it is even possible, to use the Login field as the relationship key? BTW, the entities are based on existing schema that can开发者_StackOverflow社区not be refactored at this time.


I don't believe this is possible out of the box, certainly not the ability to add a navigation property. I tried using the new power tools to reverse engineer a database and it just ignored the relationship, neither could i create it using code (annotations or fluent interface).

You can create the relationship using raw sql on the on OnModelCreating method to make the constraint, but you'd need to use join's manually to navigate between the tables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜