1

我在以下之间有一对多和多对多的关系:

public class User
{
    [Key]
    public Guid UserId { get; set; }
    public string Address { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public string Units { get; set; }
    public int Distance { get; set; }

    public List<Sport> Sports { get; set; }

    [InverseProperty("UserCreatorId")]
    public ICollection<Event> EventsCreated { get; set; }

    [InverseProperty("Users")]
    public ICollection<Event> Events { get; set; }

    public ICollection<Comment> Comments { get; set; }
}

和:

public class Event
{
    [Key]
    public int EventId { get; set; }
    public string SportName { get; set; }
    public string Explanation { get; set; }

    [Required]
    public string Address { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public bool Active { get; set; }
    public int NumUsersJoined { get; set; }
    [Required, Range(2,50, ErrorMessage = "Must be between 2 and 50")]
    public int MaxNumUsers { get; set; }

    public Guid UserCreatorId { get; set; }    

    public ICollection<User> Users { get; set; }

    public ICollection<Comment> Comments { get; set; }
}

多对多关系可以正常工作,但 1:many 不是,它在事件表 User_UserId 的数据库中创建了一个额外的列,并使其成为外键,而不是我想要的 UserCreatorId。我究竟做错了什么?

4

1 回答 1

2

尝试这样做,

Event,

 [ForeignKey("UserCreator")]
 public Guid UserCreatorId { get; set; }  

  public User UserCreator{ get; set; }  
 [InverseProperty("Events")]
public ICollection<User> Users { get; set; }

并且,在User

   [InverseProperty("UserCreator")]
    public ICollection<Event> EventsCreated { get; set; }
于 2012-05-21T11:31:04.913 回答