EF Core 2和SQL Server中3个表之间的关系

时间:2018-02-04 02:38:06

标签: sql-server many-to-many ef-core-2.0

我有表格UserDocument& Status。需要在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。

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

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

<强>上下文

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; }
}

答案 1 :(得分:0)

我在网络核心2.1中有类似的情况

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

链接实体

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);

    }
}