1

我有以下情况让我很难过:

public class SegUser
{
      public string IdUser { get; set; }

      public List<SegRol> SegRoles { get; set; }

      public virtual List<SegApps> Apps{ get; set; }
}

public class SegRole
{
     public virtual int IdRole { get; set; }

     public virtual List<SegUer> SegUsers { get; set; }

    public virtual List<SegApp> Apps { get; set; }

}

public class SegApp
{
      public virtual int IdApp{ get; set; }

      public virtual List<SegUser> Users { get; set; }

      public virtual List<SegRole> Roles { get; set; }

}

在我的数据库中,我有这 3 个表和一个带有 3 个 PK 的额外表(每个实体一个)来建立这 3 个实体之间的关系。

如何使用 Entity Framework 5 fluent API 实现映射。

我已经尝试过:

    private void MapSegUser()
    {
        //mapping of another fields

        entityConfiguration
            .HasMany(x => x.SegRoles)
            .WithMany(x => x.SegUsers)
            .Map(mc =>
            {
                mc.MapLeftKey("id_role");
                mc.MapRightKey("id_user");
                mc.ToTable("seg_users_roles");
            });

        entityConfiguration
            .HasMany(x => x.Apps)
            .WithMany(x => x.Users)
            .Map(mc =>
            {
                mc.MapLeftKey("id_app");
                mc.MapRightKey("id_user");
                mc.ToTable("seg_users_roles_apps");
            });

    }


   private void MapSegApp()
    {
        //mapping of another fields

          entityConfiguration
            .HasMany(x => x.Users)
            .WithMany(x => x.Apps)
            .Map(mc =>
            {
                mc.MapLeftKey("id_user");
                mc.MapRightKey("id_app");
                mc.ToTable("seg_users_roles_apps");
            });

    }


     private void MapSegRole()
    {
        //mapping of another fields

        entityConfiguration
            .HasMany(x => x.SegUsers)
            .WithMany(x => x.SegRoles)
            .Map(mc =>
            {
                mc.MapLeftKey("id_user");
                mc.MapRightKey("id_role");
                mc.ToTable("seg_users_roles");
            });
    }
4

0 回答 0