实体框架代码优先:自定义映射

发布于 2024-11-28 20:22:28 字数 365 浏览 5 评论 0原文

public class User
{
   public int Id {get;set;}

   public string Name {get;set}

   public ICollection<User> Followers {get;set;}
   public ICollection<User> Following {get;set;}
}

我的模型如上所示,实体框架自动在数据库中创建一个表和 UserUser ,其中包含行 User_IDUser_ID1 来映射此模型。我想自己映射该表和行。

我该怎么办,谢谢!!

public class User
{
   public int Id {get;set;}

   public string Name {get;set}

   public ICollection<User> Followers {get;set;}
   public ICollection<User> Following {get;set;}
}

My Model looks like above, Entity framework automatically creates A table and UserUser with rows User_ID and User_ID1 in DB to map this model. I want to map that table and rows myself.

How can i do that, Thanx!!

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

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

发布评论

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

评论(2

病女 2024-12-05 20:22:28

来自 Scott Gu 的博客 关于 多值关联

多对多关联
Category和Item之间是多对多的关联
关联,如上面的类图所示。多对多
关联映射隐藏了中间关联表
应用程序,这样您就不会在您的应用程序中出现不需要的实体
域模型。也就是说,在真实的系统中,您可能没有
多对多关联,因为我的经验是几乎有
始终必须附加到之间的每个链接的其他信息
关联的实例(例如添加项目的日期和时间
到一个类别)并且表示此信息的最佳方式是
通过中间关联类(在 EF 中,您可以映射
关联类作为实体并映射两个一对多关联
对于任何一方。)。

在多对多关系中,连接表(或链接表,如某些
开发人员称之为)有两列:类别的外键
和项目表。主键是两列的组合。 在EF中
Code First,多对多关联映射可以通过以下方式自定义
像这样的流畅 API 代码:

    class ItemConfiguration : EntityTypeConfiguration<Item> {
    internal ItemConfiguration()
    {
        this.HasMany(i => i.Categories)
            .WithMany(c => c.Items)
            .Map(mc =>
            {
                mc.MapLeftKey("ItemId");
                mc.MapRightKey("CategoryId");
                mc.ToTable("ItemCategory");
            });
    } }

在 DbContext 中注册此配置(您使用 DbContext api 对吧?),如下所示:

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Configurations.Add(new ItemConfiguration());
  }

祝你好运,希望这会有所帮助!

From Scott Gu's blog about Many-valued Associations:

Many-to-Many Associations
The association between Category and Item is a many-to-many
association, as can be seen in the above class diagram. a many-to-many
association mapping hides the intermediate association table from the
application, so you don’t end up with an unwanted entity in your
domain model. That said, In a real system, you may not have a
many-to-many association since my experience is that there is almost
always other information that must be attached to each link between
associated instances (such as the date and time when an item was added
to a category) and that the best way to represent this information is
via an intermediate association class (In EF, you can map the
association class as an entity and map two one-to-many associations
for either side.).

In a many-to-many relationship, the join table (or link table, as some
developers call it) has two columns: the foreign keys of the Category
and Item tables. The primary key is a composite of both columns. In EF
Code First, many-to-many associations mappings can be customized with
a fluent API code like this:

    class ItemConfiguration : EntityTypeConfiguration<Item> {
    internal ItemConfiguration()
    {
        this.HasMany(i => i.Categories)
            .WithMany(c => c.Items)
            .Map(mc =>
            {
                mc.MapLeftKey("ItemId");
                mc.MapRightKey("CategoryId");
                mc.ToTable("ItemCategory");
            });
    } }

Register this configuration in your DbContext's (you using the DbContext api right?) like this:

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Configurations.Add(new ItemConfiguration());
  }

Good luck, hope this help!

狼亦尘 2024-12-05 20:22:28

要将实体映射到其自身,您需要执行类似的操作,

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>().HasMany(u => u.Followers)
        .WithMany().ForeignKey(u => u.FollowerId);

    base.OnModelCreating(modelBuilder);
}

但如果不查看数据库模型以及如何将关注者与用户实际关联起来,就很难判断。

To map an entity to itself, you would do something like this

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>().HasMany(u => u.Followers)
        .WithMany().ForeignKey(u => u.FollowerId);

    base.OnModelCreating(modelBuilder);
}

its hard to tell without seeing your database model though, and how you actually relate the followers to the user.

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