Property name in a type must be unique

后端 未结 3 1294
情话喂你
情话喂你 2021-02-13 10:45

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; }    
          


        
相关标签:
3条回答
  • 2021-02-13 10:47

    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);
    
    0 讨论(0)
  • 2021-02-13 10:51

    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);
    
    0 讨论(0)
  • 2021-02-13 10:54

    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; }
    
    0 讨论(0)
提交回复
热议问题