一对多关系实体框架4.1 mvc的问题

发布于 2024-11-28 16:33:17 字数 495 浏览 4 评论 0原文

我有这些模型:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Books> FavoriteBooks { get; set; }
}

public class Books
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author{ get; set; }
}

当我运行 DBContext 并检查生成的表时,Books 表已创建 Student_Id 列。我希望图书表只是一个参考,而不是链接回学生。 如何使这种关系成为单向关系并且不会在 Books 中创建 Student_Id 列?我可以使用 Fluent-api 吗?或者仅在简单的数据注释中?

谢谢

i have these models:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Books> FavoriteBooks { get; set; }
}

public class Books
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author{ get; set; }
}

When i run DBContext and check the generated tables, the Books table have created Student_Id column. I want the Books table to be just a reference and not linked back to Student.
How can I make this relationship uni-directional and will have no Student_Id column created in Books? can i use fluent-api? or in simple data annotations only?

thanks

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

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

发布评论

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

评论(1

绿光 2024-12-05 16:33:17

在 Fluent API 中,它应该看起来像这样:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
                .HasMany(s => s.FavoriteBooks)
                .WithMany();
}

这将为您创建一个“StudentBooks”表来维护多对多关系。这不是您的问题所暗示的一对多关系,因为一个学生可以有很多最喜欢的书,但每本书可能是许多学生最喜欢的。

更新

为了完整起见,如果您已经定义了该链接表并且需要告诉 EF 映射是什么样子,则可以执行以下操作:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
                .HasMany(s => s.FavoriteBooks)
                .WithMany();
                .Map(cfg => cfg.MapLeftKey("Student_Id")
                               .MapRightKey("Books_Id")
                               .ToTable("StudentBooks"));
}

In the fluent API it should look something like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
                .HasMany(s => s.FavoriteBooks)
                .WithMany();
}

That'll create a "StudentBooks" table for you to maintain the many-to-many relationship. It's not a one-to-many relationship as your question suggests, since a student can have many favourite books, but each book might be a favourite of many students.

Update

For the sake of completeness, here's how you'd do it if you already have that linking table defined and need to tell EF what the mapping looks like:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
                .HasMany(s => s.FavoriteBooks)
                .WithMany();
                .Map(cfg => cfg.MapLeftKey("Student_Id")
                               .MapRightKey("Books_Id")
                               .ToTable("StudentBooks"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文