使用 EF Code First 开发的 SQL CE 数据库中的多对一外键

发布于 2024-11-16 03:04:04 字数 993 浏览 3 评论 0原文

这是我第一次尝试 SQL CE 或 EF,所以我可能有很多误解。我搜索了很多博客文章,但似乎仍然无法正确理解这一点。

我有一个 MVC3 网站,用于注册我们正在举办的比赛。我有一个 RaceEvents 表和一个 Runners 表,其中每个 RaceEvent 都会有许多跑步者为其注册,即多对一。以下是删除了无关数据的 POCO:

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

据我所知,其中应该可以工作。据我了解,按照惯例,应该认为 RaceEventId 是 RaceEvents 的外键。如果这还不够好,我会用ForeignKey 属性来告诉它。

但是,它似乎不起作用。每当我插入一个新的跑步者时,它也会在 RaceEvents 表中插入一个新条目。当我在 ServerExplorer 中查看表图时,它在 Runners 表图的顶部显示了两个金钥匙,一个用于 Id,在属性中标识为 PrimaryKey,另一个用于 RaceEventId,未标识为 PrimaryKey,但属性中指示适用于表 Runners,而不是表 RaceEvents。我期望 Id 是金钥匙,但 RaceEventId 是银外键。

FWIW,我并不真正关心 RaceEvent 中的 ICollection,但博客文章似乎都暗示它是必要的。

有人能帮我解决这个问题吗?

谢谢。

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 RaceEvents. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入怼 2024-11-23 03:04:04

好吧,

抱歉我没有足够详细地阅读你的问题。在我们的项目中,这就是我们代表您所做的事情的方式。我查看了 SSMS,它没有显示所述灰色键,但它不会在每次添加跑步者时创建比赛事件。尽管您确实需要确保在创建跑步者时设置了比赛事件属性。

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

}

有任何问题请告诉我。

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.

听不够的曲调 2024-11-23 03:04:04

您需要覆盖 DbContext 中创建的模型。以下是 AnsNet_User 和 AnsNet_User 的示例: AspNet_Roles N:N 关系

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文