1

私は次の間に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:多はそうではありません。イベントテーブル 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 に答える