EF Core 2 中的 3 个表与 SQL 服务器之间的关系

Relationship between 3 tables in EF Core 2 and SQL Server

我有表 UserDocumentStatus。需要在 EF Core 2.0 中定义一个关系,它可以给我如下数据:

Document               ReferredBy           AssignedTo          Status
DOC-0001(DocumentId)   USR-0001(UserId)     USR-0002(UserId)    STA-001(statusId)
DOC-0002(DocumentId)   USR-0002(UserId)     USR-0001(UserId)    STA-002(statusId)

有人可以帮忙创建这个吗?我正在使用 SQL Server 2016。

非常感谢任何帮助。

获得所需内容的最简单方法是根据需要在测试数据库中创建这三个表,然后将数据库搭建到项目中并查看输出。

上下文

public partial class DatabaseContext : DbContext
{
    public virtual DbSet<Document> Document { get; set; }
    public virtual DbSet<Status> Status { get; set; }
    public virtual DbSet<User> User { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"<ConnectionString>;");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Document>(entity =>
        {
            entity.Property(e => e.DocumentId).ValueGeneratedNever();

            entity.Property(e => e.ReferredById).ValueGeneratedOnAdd();

            entity.HasOne(d => d.AssignedTo)
                .WithMany(p => p.DocumentAssignedTo)
                .HasForeignKey(d => d.AssignedToId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Document_User1");

            entity.HasOne(d => d.ReferredBy)
                .WithMany(p => p.DocumentReferredBy)
                .HasForeignKey(d => d.ReferredById)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Document_User");

            entity.HasOne(d => d.Status)
                .WithMany(p => p.Document)
                .HasForeignKey(d => d.StatusId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Document_Status");
        });

        modelBuilder.Entity<Status>(entity =>
        {
            entity.Property(e => e.StatusName).IsRequired();
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.Property(e => e.UserName).IsRequired();
        });
    }
}

文档

public partial class Document
{
    public int DocumentId { get; set; }
    public int ReferredById { get; set; }
    public int AssignedToId { get; set; }
    public int StatusId { get; set; }

    public User AssignedTo { get; set; }
    public User ReferredBy { get; set; }
    public Status Status { get; set; }
}

状态

public partial class Status
{
    public Status()
    {
        Document = new HashSet<Document>();
    }

    public int StatusId { get; set; }
    public string StatusName { get; set; }

    public ICollection<Document> Document { get; set; }
}

用户

public partial class User
{
    public User()
    {
        DocumentAssignedTo = new HashSet<Document>();
        DocumentReferredBy = new HashSet<Document>();
    }

    public int UserId { get; set; }
    public string UserName { get; set; }

    public ICollection<Document> DocumentAssignedTo { get; set; }
    public ICollection<Document> DocumentReferredBy { get; set; }
}

我在net core 2.1也有类似情况

这是我对 3 个实体的解决方案。 Empresa、Usuario、Perfil

Link实体

public class EmpresaUsuarioPerfil
{
    public int EmpresaId { get; set; }
    public int UsuarioId { get; set; }
    public string PerfilId { get; set; }

    public Empresa Empresa { get; set; }
    public Perfil Perfil { get; set; }
    public Usuario Usuario { get; set; }
}

Empresa

public class Empresa
{
    public int EmpresaId { get; set; }
    public string Nombre { get; set; }

    public virtual ICollection<EmpresaUsuarioPerfil> EmpresaUsuarioPerfil { get; set; }

}

Usuario

public class Usuario
{
    public int UsuarioId { get; set; }
    public string Nombre { get; set; }

    public virtual ICollection<EmpresaUsuarioPerfil> EmpresaUsuarioPerfil { get; set; }

}

Perfil

public class Perfil
{
    public string PerfilId { get; set; }
    public string Descripcion { get; set; }

    public virtual ICollection<EmpresaUsuarioPerfil> EmpresaUsuarioPerfil { get; set; }
}

数据库上下文

public class AplicacionContext : DbContext
{
    public DbSet<Empresa> Empresas { get; set; }
    public DbSet<Perfil> Perfiles { get; set; }
    public DbSet<Usuario> Usuarios { get; set; }
    public DbSet<EmpresaUsuarioPerfil> EmpresaUsuarioPerfil { get; set; }

    public AplicacionContext() : base()
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<EmpresaUsuarioPerfil>()
            .HasKey(x => new { x.EmpresaId, x.UsuarioId, x.PerfilId  });

        modelBuilder.Entity<EmpresaUsuarioPerfil>()
            .HasOne(bc => bc.Empresa)
            .WithMany(b => b.EmpresaUsuarioPerfil)
            .HasForeignKey(bc => bc.EmpresaId);

        modelBuilder.Entity<EmpresaUsuarioPerfil>()
            .HasOne(bc => bc.Perfil)
            .WithMany(b => b.EmpresaUsuarioPerfil)
            .HasForeignKey(bc => bc.PerfilId);

        modelBuilder.Entity<EmpresaUsuarioPerfil>()
            .HasOne(bc => bc.Usuario)
            .WithMany(c => c.EmpresaUsuarioPerfil)
            .HasForeignKey(bc => bc.UsuarioId);

    }
}