实体框架5流畅地映射了3个实体的多对多

时间:2013-07-06 21:04:25

标签: many-to-many entity-framework-5

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

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流畅的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");
            });
    }

0 个答案:

没有答案