【问题标题】:How to Create Tables Automatically in Code First Approach using PostgreSql如何使用 PostgreSql 在代码优先方法中自动创建表
【发布时间】:2018-11-14 14:52:51
【问题描述】:

我正在尝试使用代码优先方法和 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);
        }
    }

【问题讨论】:

    标签: postgresql asp.net-core-2.0


    【解决方案1】:

    我认为扩展 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...      
        }
    }}
    

    【讨论】:

    • 嗨 keuleJ,感谢您的更新。我仍然遇到同样的错误。
    • 您是否删除了所有带有 Asp* 类的 DbSet?
    • 也许您应该删除您拥有的表格并让它们重新生成。我有以下表格: AspNetRoleClaims AspNetRoles AspNetUserClaims AspNetUserLogins AspNetUserRoles AspNetUsers AspNetUserTokens
    • 我已经解决了最初的问题。现在我收到以下错误。无法确定“ICollection”类型的导航属性“AspNetUser.Projects”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。
    • 您应该使用扩展 IdentityUser 的自定义用户类并在那里为您的类进行映射。然后让你的 ApplicationDbContext : IdentityDbContext
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多