-1

I have a class called ProjectMember that's a many:many between Project and Freelancer classes. Whenever I try to use Update-Database I get the error in the title. I tried many things that didn't seem to work.

This is the code for the classes

    public class Project
    {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        public string Description { get; set; }
        [Required]
        public string ClientId { get; set; }
        public Client Client { get; set; }
        public virtual ICollection<ProjectMember> Members { get; set; }
    }
public class Freelancer
    {
        public string Id { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public DateTime DateOfBirth { get; set; }
        public bool IsFullTime { get; set; }
        [Required]
        public string JobTitle { get; set; }
        [Required]
        public int JobCategoryId { get; set; }
        public JobCategory JobCategory { get; set; }
        public virtual ICollection<ProjectMember> Projects { get; set; }
    }
    public class ProjectMember
    {
        public int ProjectId { get; set; }
        public Project Project { get; set; }
        public string FreelancerId { get; set; }
        public Freelancer Freelancer { get; set; }
        [Required]
        public int ReqWeeklyHours { get; set; }
    }

This is my DbContext Class

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

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

            builder.Entity<ProjectMember>().HasKey(pm => new { pm.ProjectId, pm.FreelancerId });

            
        }

        public DbSet<Client> Clients { get; set; }
        public DbSet<Freelancer> Freelancers { get; set; }
        public DbSet<JobCategory> JobCategories { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<ProjectMember> ProjectMembers { get; set; }
    }

This is the code produced in the migration regarding the ProjectMembers Table


            migrationBuilder.CreateTable(
                name: "ProjectMembers",
                columns: table => new
                {
                    ProjectId = table.Column<int>(type: "int", nullable: false),
                    FreelancerId = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    ReqWeeklyHours = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProjectMembers", x => new { x.ProjectId, x.FreelancerId });
                    table.ForeignKey(
                        name: "FK_ProjectMembers_Freelancers_FreelancerId",
                        column: x => x.FreelancerId,
                        principalTable: "Freelancers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ProjectMembers_Projects_ProjectId",
                        column: x => x.ProjectId,
                        principalTable: "Projects",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

0 Answers0