With the following database schema:
User: UserID (primary key)
UserInfo: UserInfoID (primary key), UserID (foreignKey), [Other columns with UserInfo data]
User and UserInfo have a 1-1 relationship. I want to map this schema to the following POCOs:
public class User
{
[Key]
public int UserID {get;set;}
public virtual UserInfo userinfo {get;set;}
}
public class UserInfo
{
[Key]
public int UserInfoID {get;set;}
public int UserID {get;set;}
[ForeignKey("UserID")]
public virtual User User {get; set;}
... Other properties of UserInfo...
}
Basically I want to be able to load a Users object, and have the related UserInfo object present as well. This seems like a simple task, but I have not been able to find the right combination of attributes and/or fluent API to accomplish this.
EDIT: I have found that making the UserInfo property in Users an ICollection, then everything is wired up properly. So my question now is, is there a way to avoid using an ICollection, as I know that a User will have either 1 or 0 associated UserInfo records.
For one to one scenarios you have to use the same property as key and fk on both entities.
Try using the UserId property as the Id of UserInfo entity and then like the Fk to the User property.