同一型号中的一对多+多对多

时间:2012-05-21 11:21:15

标签: c# .net asp.net-mvc-3 entity-framework ef-code-first

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

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。我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试这样做,

<{1>}中的

Event

和, 在 [ForeignKey("UserCreator")] public Guid UserCreatorId { get; set; } public User UserCreator{ get; set; } [InverseProperty("Events")] public ICollection<User> Users { get; set; }

User