执行 DbCommand 时出错-引入外键约束失败

huangapple go评论77阅读模式
英文:

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);

Help link about reason error

答案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);

huangapple
  • 本文由 发表于 2020年1月6日 17:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定