I am using Entity Framework 5 and I have the following entities:
public class User {
public Int32 Id { get; set; }
public String Username { get; set; }
MapKey
is only used if your foreign key column is not exposed as a property in your model. But in your case it is - as property Claim.Id
. In that case you must use HasForeignKey
instead of MapKey
:
HasRequired<User>(x => x.User)
.WithMany(y => y.Claims)
.HasForeignKey(x => x.Id);
In addition to Slauma's answer
HasForeignKey
is not available for 1:1, if you still need it you can simply use WithMany
without parameters
HasRequired<User>(x => x.User).WithMany().HasForeignKey(x => x.Id);
HasRequired(x => x.City).WithMany().HasForeignKey(x => x.CityId);
This may happen also when there is a mismatch in the type of the referencing property. For example:
public string TipId { get; set; }
instead of
public int TipId { get; set; }