开发者

many-to-one ForeignKeys in SQL CE db developed with EF Code First

开发者 https://www.devze.com 2023-03-13 18:19 出处:网络
This is my first foray in either SQL CE or EF, so I may have a lot of misunderstandings. I\'ve searched a lot of blog entries but still can\'t seem to get this right.

This is my first foray in either SQL CE or EF, so I may have a lot of misunderstandings. I've searched a lot of blog entries but still can't seem to get this right.

I have an MVC3 web site for registrations for a race we're running. I have a RaceEvents table, and a Runners table, where each RaceEvent will have many runners registered it for it, i.e., Many-to-One. Here are the POCO's with extraneous data stripped out:

  public class RaceEvent
{
    [Required]
    public int Id { get; set; }

    public virtual ICollection<Runner> Runners { get; set; }

}

    public class Runner
{
    [Required]
    public int Id { get; set; }
    [Required]
    public int RaceEventId { get; set;}
    [ForeignKey("RaceEventId")]
    public RaceEvent RaceEvent { get; set; }
}

Which, as much as I can figure out, ought to work. As I understand it, it should figure out by convention that RaceEventId is a foreign key to RaceE开发者_运维百科vents. And if that's not good enough, I'm telling it with the ForeignKey attribute.

However, it doesn't seem to be working. Whenever I insert a new runner, it is also inserting a new entry in the RaceEvents table. And when I look at the table diagram in ServerExplorer, it shows two gold keys at the top of the Runners table diagram, one for Id, identified in the properties as a PrimaryKey, and the other for RaceEventId, not identified as a PrimaryKey, but indicated in the properties to be for table Runners, rather than for table RaceEvents. I would expect a gold key for Id, but a silver ForeignKey for RaceEventId.

FWIW, I don't really care about the ICollection in the RaceEvent, but the blog entries all seemed to imply that it was necessary.

Can anybody help me get this right?

Thanks.


Ok,

Sorry I did not read your question in enough detail. In our project this is how we would represent what your doing. I looked in SSMS and it is not showing said grey key, but it does not create a race event every time you add a runner. Although you do need to make sure when you create a runner that you set the race event property.

public class DB : DbContext
{
    public DB()
        : base("Data Source=(local);Initial Catalog=DB;Integrated Security=True")
    {
    }

    public IDbSet<Runner> Runners { get; set; }
    public IDbSet<RaceEvent> RaceEvents { get; set; }
}

public class RaceEvent
{
    [Key]
    public int RaceEventID { get; set; }


}

public class Runner
{
    [Key]
    public int RunnerID { get; set; }

    [Required]
    public virtual RaceEvent RaceEvent { get; set; }

}

Any question let me know.


You need to override the model creating in the DbContext. Below is a sample for AnsNet_User & AspNet_Roles N:N relationship

protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
    dbModelBuilder.Entity<aspnet_Users>().HasMany(a => a.aspnet_Roles).WithMany(b => 
    b.aspnet_Users).Map( 
        m =>
        {
            m.MapLeftKey("UserId");
            m.MapRightKey("RoleId");
            m.ToTable("aspnet_UsersInRoles");
        });

    base.OnModelCreating(dbModelBuilder);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号