I was hoping this would be fairly straight forward but I haven't had much luck. The database I'm working with has a table for users and a table for tokens, they relate to one another with a 1 to 1 relationship with tokens being as I understand it the principal.
public class Token
{
public int ID { get; set; }
public string Location { get; set; }
public virtual User Contact { get; set; }
}
public class User
{
public int ID { get; set; }
public int TokenID { get; set; }
public string Name { get; set; }
public virtual Token Token { get; set; }
}
So every User has an associated Token which belongs to that User only, and there also also other Tokens that relate to other objects which I will be mapping as well in a similar fashion.
So far I have tried below as well as some variations and it hasn't worked as expected, I keep getting incorrect locations for users as it seems to be using User.ID to map to Token.ID instead of using User.TokenID.
modelBuilder.Entity<User>()
.HasRequired(p => p.Token)
.WithOptional(p => p.Contact);
I'm thinking I'm missing actually specifying the columns to use to map? I'm just unsure where to go from here.
Edit Previously when mapping relationships that use the WithRequired() extension method I've then been able to specify HasForeignKey() afterwards but WithOptional() doesn't seem to have this functionality.