如何使用PostgreSql以代码优先方式自动创建表

时间:2018-11-14 06:43:55

标签: postgresql asp.net-core-2.0

我正在尝试使用代码优先方法对postgresql数据库创建表。我的数据库上下文如下。在进行迁移时,出现以下错误。 不能将表'AspNetRoles'用于实体类型'AspNetRole',因为它已用于实体类型'IdentityRole',并且它们的主键之间没有关系。

如何解决这个问题?

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Project> Projects { get; set; }
        public DbSet<Story> Stories { get; set; }
        public DbSet<Epic> Epics { get; set; }
        public DbSet<Favourite> Favourites { get; set; }
        public DbSet<Priority> Priorities { get; set; }
        public DbSet<ProjectPermission> ProjectPermission { get; set; }
        public DbSet<Theme> Themes { get; set; }
        public DbSet<AspNetRole> AspNetRoles { get; set; }
        public DbSet<AspNetRoleClaim> AspNetRoleClaims { get; set; }
        public DbSet<AspNetUser> AspNetUsers { get; set; }
        public DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
        public DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
        public DbSet<AspNetUserToken> AspNetUserTokens { get; set; }
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<AspNetUserToken>(entity =>
            {
                entity.HasKey(e => e.UserId);
                entity.HasKey(e => e.LoginProvider);
                entity.HasKey(e => e.Name);

            });

            builder.Entity<AspNetRole>(entity =>
            {
                entity.HasKey(e => e.Id);
            });

            builder.Entity<AspNetRoleClaim>(entity =>
            {
                entity.HasKey(e => e.Id);
            });

            builder.Entity<AspNetUser>(entity =>
            {
                entity.HasKey(e => e.Id);
            });

            builder.Entity<AspNetUserClaim>(entity =>
            {
                entity.HasKey(e => e.Id);
            });

            builder.Entity<AspNetUserLogin>(entity =>
            {
                entity.HasKey(e => e.ProviderKey);
                entity.HasKey(e => e.LoginProvider);

                entity.HasIndex(e => e.UserId)
                    .HasName("fki_FK_AspNetUserLoginUserId");

            });
            builder.Entity<Project>(entity =>
            {
                entity.HasKey(e => e.ProjectId);

                entity.HasIndex(e => e.CreatedBy)
                    .HasName("fki_FK_ProjectCreatedBy");

                entity.HasIndex(e => e.Owner)
                    .HasName("fki_FK_ProjectOwner");


                entity.HasOne(d => d.AspNetUser)
                    .WithMany(p => p.Projects)
                    .HasForeignKey(d => d.CreatedBy)
                    .HasForeignKey(a => a.Owner)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ProjectCreatedBy");
            });

            builder.Entity<Story>(entity =>
            {
                entity.HasKey(e => e.StoryId);

                entity.HasIndex(e => e.ProjectId)
                    .HasName("fki_FK_StoryProjectId");

                entity.HasIndex(e => e.ThemeId)
                    .HasName("fki_FK_StoryThemeId");

                entity.HasIndex(e => e.EpicId)
                    .HasName("fki_FK_StoryEpicId");

                entity.HasIndex(e => e.PriorityId)
                    .HasName("fki_FK_StoryPriorityId");

                entity.HasIndex(e => e.CreatedBy)
                    .HasName("fki_FK_StoryCreatedBy");

                entity.HasOne(d => d.AspNetUser)
                    .WithMany(p => p.Stories)
                    .HasForeignKey(d => d.CreatedBy)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_StoryCreatedBy");

                entity.HasOne(d => d.Project)
                    .WithMany(p => p.Stories)
                    .HasForeignKey(d => d.ProjectId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_StoryProjectId");

                entity.HasOne(d => d.Epic)
                    .WithMany(p => p.Stories)
                    .HasForeignKey(d => d.EpicId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_StoryEpicId");

                entity.HasOne(d => d.Theme)
                    .WithMany(p => p.Stories)
                    .HasForeignKey(d => d.ThemeId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_StoryThemeId");

                entity.HasOne(d => d.Priority)
                    .WithMany(p => p.Stories)
                    .HasForeignKey(d => d.PriorityId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_StoryPriorityId");
            });

            builder.Entity<Epic>(entity =>
            {
                entity.HasKey(e => e.EpicId);

                entity.HasIndex(e => e.CreatedBy)
                    .HasName("fki_FK_EpicCreatedBy");

                entity.HasIndex(e => e.ProjectId)
                    .HasName("fki_FK_EpicProjectId");


                entity.HasOne(d => d.AspNetUser)
                    .WithMany(p => p.Epics)
                    .HasForeignKey(d => d.CreatedBy)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_EpicCreatedBy");

                entity.HasOne(d => d.Project)
                    .WithMany(p => p.Epics)
                    .HasForeignKey(d => d.ProjectId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_EpicProjectId");
            });

            builder.Entity<Favourite>(entity =>
            {
                entity.HasKey(e => e.FavouriteId);

                entity.HasIndex(e => e.UserId)
                    .HasName("fki_FK_FavouriteUserId");

                entity.HasIndex(e => e.ProjectId)
                    .HasName("fki_FK_FavouriteProjectId");


                entity.HasOne(d => d.Project)
                    .WithMany(p => p.Favourites)
                    .HasForeignKey(d => d.ProjectId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_FavouriteProjectId");
            });

            builder.Entity<Priority>(entity =>
            {
                entity.HasKey(e => e.PriorityId);

                entity.HasIndex(e => e.CreatedBy)
                    .HasName("fki_FK_PriorityCreatedBy");


                entity.HasOne(d => d.AspNetUser)
                    .WithMany(p => p.Priorities)
                    .HasForeignKey(d => d.CreatedBy)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_PriorityCreatedBy");
            });
            builder.Entity<ProjectPermission>(entity =>
            {
                entity.HasKey(e => e.PermissionId);

                entity.HasIndex(e => e.ProjectId)
                    .HasName("fki_FK_ProjectPermissionProjectId");

                entity.HasOne(d => d.Project)
                    .WithMany(p => p.ProjectPermissions)
                    .HasForeignKey(d => d.ProjectId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ProjectPermissionProjectId");
            });

            builder.Entity<Theme>(entity =>
            {
                entity.HasKey(e => e.ThemeId);

                entity.HasIndex(e => e.CreatedBy)
                    .HasName("fki_FK_ThemesCreatedBy");

                entity.HasIndex(e => e.ProjectId)
                    .HasName("fki_FK_ThemesProjectId");

                entity.HasOne(d => d.AspNetUser)
                    .WithMany(p => p.Themes)
                    .HasForeignKey(d => d.CreatedBy)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ThemesCreatedBy");

                entity.HasOne(d => d.Project)
                    .WithMany(p => p.Themes)
                    .HasForeignKey(d => d.ProjectId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ThemesProjectId");
            });
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为扩展IdentityDbContext比在自己的类中拥有带有身份类的DbSet更好。

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Test
{
public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
    public DbSet<Project> Projects { get; set; }
    ...

    public KassandraContext(DbContextOptions<KassandraContext> options)
        : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(modelBuilder);  
        //Your stuff goes here...      
    }
}}