Here showing following error when going to update-database, through dotnet-core migration.
Failed executing DbCommand (48ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [DepartmentSchool] (
[DepartmentID] uniqueidentifier NOT NULL,
[SchoolsId] uniqueidentifier NOT NULL,
[Id] uniqueidentifier NOT NULL,
CONSTRAINT [PK_DepartmentSchool] PRIMARY KEY ([SchoolsId], [DepartmentID]),
CONSTRAINT [FK_DepartmentSchool_Department_DepartmentID] FOREIGN KEY ([DepartmentID]) REFERENCES [Department] ([ID]) ON DELETE CASCADE,
CONSTRAINT [FK_DepartmentSchool_School_SchoolsId] FOREIGN KEY ([SchoolsId]) REFERENCES [School] ([ID]) ON DELETE CASCADE
);
Below is the entity relation class are:
School Class(First Table):
public partial class Schools
{
public Guid ID { get; set; }
public string Name { get; set; }
public Guid? CountryId { get; set; }
public Country Country { get; set; }
public ICollection<DepartmentSchool> DepartmentSchools { get; set; }
}
Department Class(Second Table):
public partial class Department
{
public Guid ID { get; set; }
public string Title { get; set; }
public DateTime CreatedAt { get; set; }
public ICollection<DepartmentSchool> DepartmentSchools { get; set; }
}
DepartmentSchool Class (Middle table)
public class DepartmentSchool
{
public Guid Id { get; set; }
public Guid DepartmentID { get; set; }
public Department Department { get; set; }
public Guid SchoolsId { get; set; }
public Schools Schools { get; set; }
}
and in the modulbulider it is relation is define here:
//Many to many relationship between School and Depatment
modelBuilder.Entity<DepartmentSchool>()
.HasKey(ds => new { ds.SchoolsId, ds.DepartmentID });
modelBuilder.Entity<DepartmentSchool>()
.HasOne(ds => ds.Department)
.WithMany(d => d.DepartmentSchools)
.HasForeignKey(ds => ds.DepartmentID);
modelBuilder.Entity<DepartmentSchool>()
.HasOne(ds => ds.Schools)
.WithMany(d => d.DepartmentSchools)
.HasForeignKey(ds => ds.SchoolsId);