英文:
Failed executing DbCommand-Introducing FOREIGN KEY constraint
问题
Here's the translation of the code portion you provided:
以下是您提供的代码部分的翻译:
在进行dotnet-core迁移时,更新数据库时出现以下错误。
执行DbCommand失败(48毫秒)[参数 = [],CommandType = 'Text',CommandTimeout = '30']
创建表[DepartmentSchool] (
[DepartmentID]唯一标识符 NOT NULL,
[SchoolsId]唯一标识符 NOT NULL,
[Id]唯一标识符 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
);
以下是实体关系类:
**School**类(第一张表):
```csharp
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类(第二张表):
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类(中间表):
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; }
}
在模型生成器中,它的关系定义如下:
// School和Department之间的多对多关系
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);
这是您提供的代码的翻译。
英文:
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);
答案1
得分: 3
禁用级联删除,允许引用中的空值。
.OnDelete(DeleteBehavior.Restrict);
关于在Asp Core中的级联删除的更多信息,请参考关于级联删除的说明。
例如:
modelBuilder.Entity<Invoice>()
.HasOne(i => i.Customer)
.WithMany(c => c.Invoices)
.OnDelete(DeleteBehavior.Restrict);
有关错误原因的帮助链接,请查看错误原因的帮助链接。
英文:
Disable cascading delete by allowing null values in the references.
.OnDelete(DeleteBehavior.Restrict);
About Cascade Delete In Asp Core
For example:
modelBuilder.Entity<Invoice>()
.HasOne(i => i.Customer)
.WithMany(c => c.Invoices)
.OnDelete(DeleteBehavior.Restrict);
答案2
得分: 0
I added the code according to the accepted answer and it is working:
modelBuilder.Entity
.HasKey(ds => new { ds.SchoolsId, ds.DepartmentID });
modelBuilder.Entity
.HasOne(ds => ds.Department)
.WithMany(d => d.DepartmentSchools)
.OnDelete(DeleteBehavior.Restrict)
.HasForeignKey(ds => ds.DepartmentID);
modelBuilder.Entity
.HasOne(ds => ds.Schools)
.WithMany(d => d.DepartmentSchools)
.OnDelete(DeleteBehavior.Restrict)
.HasForeignKey(ds => ds.SchoolsId);
英文:
I added the code according to the accepted answer and it is working:
modelBuilder.Entity<DepartmentSchool>()
.HasKey(ds => new { ds.SchoolsId, ds.DepartmentID });
modelBuilder.Entity<DepartmentSchool>()
.HasOne(ds => ds.Department)
.WithMany(d => d.DepartmentSchools)
.OnDelete(DeleteBehavior.Restrict)
.HasForeignKey(ds => ds.DepartmentID);
modelBuilder.Entity<DepartmentSchool>()
.HasOne(ds => ds.Schools)
.WithMany(d => d.DepartmentSchools)
.OnDelete(DeleteBehavior.Restrict)
.HasForeignKey(ds => ds.SchoolsId);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论