Not able to create user defined tables automatically in Code First Approach using PostgreSql










1















I am trying to create the tables using code first approach with postgresql database. My DB context is below.While doing migration i am getting the below error.




Unable to determine the relationship represented by navigation
property 'AspNetUser.Projects' of type 'ICollection'. Either
manually configure the relationship, or ignore this property using the
'[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in
'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");
);











share|improve this question




























    1















    I am trying to create the tables using code first approach with postgresql database. My DB context is below.While doing migration i am getting the below error.




    Unable to determine the relationship represented by navigation
    property 'AspNetUser.Projects' of type 'ICollection'. Either
    manually configure the relationship, or ignore this property using the
    '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in
    '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");
    );











    share|improve this question


























      1












      1








      1








      I am trying to create the tables using code first approach with postgresql database. My DB context is below.While doing migration i am getting the below error.




      Unable to determine the relationship represented by navigation
      property 'AspNetUser.Projects' of type 'ICollection'. Either
      manually configure the relationship, or ignore this property using the
      '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in
      '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");
      );











      share|improve this question
















      I am trying to create the tables using code first approach with postgresql database. My DB context is below.While doing migration i am getting the below error.




      Unable to determine the relationship represented by navigation
      property 'AspNetUser.Projects' of type 'ICollection'. Either
      manually configure the relationship, or ignore this property using the
      '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in
      '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");
      );








      postgresql entity-framework asp.net-core






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 9:10









      Foo

      1




      1










      asked Nov 14 '18 at 8:10









      sathya nagarajansathya nagarajan

      255




      255






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295590%2fnot-able-to-create-user-defined-tables-automatically-in-code-first-approach-usin%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295590%2fnot-able-to-create-user-defined-tables-automatically-in-code-first-approach-usin%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線