无法使用PostgreSql在Code First方法中自动创建用户定义的表

时间:2018-11-14 08:10:39

标签: postgresql entity-framework asp.net-core

我正在尝试使用代码优先方法对postgresql数据库创建表。我的数据库上下文如下。在进行迁移时,出现以下错误。

  

无法确定导航所代表的关系   属性“ ICollection”的属性“ AspNetUser.Projects”。要么   手动配置关系,或使用   '[NotMapped]'属性或通过使用'EntityTypeBuilder.Ignore'   “ OnModelCreating”。

 public class TaskPlannerDbContext : DbContext
    {
        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 TaskPlannerDbContext(DbContextOptions<TaskPlannerDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder); 

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

                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.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.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.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.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.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.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");
            });     
        }
    }

0 个答案:

没有答案