问题:ASP.NET Core 6 MVC 和 EF Core 中的表之间外键关系

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

Problem foreign key between tables in ASP.NET Core 6 MVC & EF Core

问题

我在我的新项目中遇到了问题,我在ASP.NET Core 6和Entity Framework Core中工作。在我的项目中,我有10个表,问题出现在所有表之间的关系上。上次我发布了我的项目的一部分,你提出了一些更改和建议,我做出了更改,但我发现了另一种问题,我努力尝试解决它,但无法解决。

我决定与您分享我的整个项目,我需要一些帮助,请。

这个项目是关于学校管理系统

模型:

public class Classe
{
    [Key]
    public int Id { get; set; }
    public string ClassName { get; set; }
    public ICollection<Examen> Examens { get; set; }
    public ICollection<Subject> Subjects { get; set; }
    public ICollection<Student> Students { get; set; }
    public ICollection<TeacherSubject> TeacherSubjects { get; set; }
    public ICollection<Expense> Expenses { get; set; }
    public ICollection<Fee> Fees { get; set; }
    public ICollection<StudentAttendence> StudentAttendence { get; set; }
}

public class Examen
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Marks { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }

    public virtual Subject Subject { get; set; }
    [ForeignKey(nameof(Subject))]
    public int SubjectId { get; set; }
}

// 其余模型类的定义省略

我的DbContext:

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
    {
    }

    public DbSet<Classe> Classes { get; set; }
    public DbSet<Examen> Examens { get; set; }
    public DbSet<Subject> Subjects { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<TeacherSubject> TeacherSubjects { get; set; }
    public DbSet<Expense> Expenses { get; set; }
    public DbSet<Fee> Fees { get; set; }
    public DbSet<StudentAttendence> StudentAttendences { get; set; }
    public DbSet<TeacherAttendence> TeacherAttendences { get; set; }
}

当我添加迁移时,我总是遇到这个问题:

引入FOREIGN KEY约束'FK_Examens_Subjects_SubjectId'到'Exams'表可能导致循环或多次级联访问。请指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
无法创建约束或索引。

我在Excel中表示了我的数据库。

能否有人帮助我找出如何修复这个问题?

提前感谢!

英文:

I have a problem in my new project, I work in ASP.NET Core 6 and Entity Framework Core. In my project I have 10 tables, the problem comes from the relation between all the tables. Last time I posted a part of my project, you proposed to me some changes and advice, I make the changes but I found another type of problem, I tried very hard to solve it but I cannot.

I decided to share with you all my project and I need some helps please.

The project is about a school management system

Models:

public class Classe
{
    [Key]
    public int Id { get; set; }
    public string  ClassName { get; set; }
    public ICollection&lt;Examen&gt; Examens { get; set; }
    public ICollection&lt;Subject&gt; Subjects { get; set; }
    public ICollection&lt;Student&gt; Students { get; set; }
    public ICollection&lt;TeacherSubject&gt; TeacherSubjects { get; set; }
    public ICollection&lt;Expense&gt; Expenses { get; set; }
    public ICollection&lt;Fee&gt; Fees { get; set; }
    public ICollection&lt;StudentAttendence&gt; StudentAttendence { get; set; }
}

public class Examen
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Marks { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }

    public virtual Subject Subject { get; set; }
    [ForeignKey(nameof(Subject))]
    public int SubjectId { get; set; }
}

public class Expense
{
    public int Id { get; set; }
    public int ChargeAmount { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }

    public virtual Subject Subject { get; set; }
    [ForeignKey(nameof(Subject))]
    public int SubjectId { get; set; }
}

public class Fee
{
    public int Id { get; set; }
    public int FeeAmount { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }
}

public class Student
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
    public string Gender { get; set; }
    public string Mobile { get; set; }
    public string RollNo { get; set; }
    public string Adresse { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }
}

public class StudentAttendence
{
    public int Id { get; set; }
    public string RollNo { get; set; }
    public byte Status { get; set; }
    public DateTime date { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }

    public virtual Subject Subject { get; set; }
    [ForeignKey(nameof(Subject))]
    public int SubjectId { get; set; }
}

public class Subject
{
    public int Id { get; set; }
    public string SubjectName { get; set; }
    public ICollection&lt;Examen&gt; Examens { get; set; }
    public ICollection&lt;TeacherSubject&gt; TeacherSubjects { get; set; }
    public ICollection&lt;Expense&gt; Expenses { get; set; }
    public ICollection&lt;StudentAttendence&gt; StudentAttendence { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }
}

public class Teacher
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
    public string Gender { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public string Adresse { get; set; }
    public string PassWord { get; set; }
    public ICollection&lt;TeacherSubject&gt; TeacherSubjects { get; set; }
    public ICollection&lt;TeacherAttendence&gt; TeacherAttendence { get; set; }
}

public class TeacherAttendence
{
    public int Id { get; set; }
    public byte Status { get; set; }
    public DateTime date { get; set; }

    public virtual Teacher Teacher { get; set; }
    [ForeignKey(nameof(Teacher))]
    public int TeacherId { get; set; }
}

public class TeacherSubject
{
    public int Id { get; set; }

    public virtual Classe Classe { get; set; }
    [ForeignKey(nameof(Classe))]
    public int ClasseId { get; set; }

    public virtual Subject Subject { get; set; }
    [ForeignKey(nameof(Subject))]
    public int SubjectId { get; set; }

    public virtual Teacher Teacher { get; set; }
    [ForeignKey(nameof(Teacher))]
    public int TeacherId { get; set; }
}

My DbContext :

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions&lt;SchoolContext&gt; options):base(options)
    {
    }

    public DbSet&lt;Classe&gt; Classes { get; set; }
    public DbSet&lt;Examen&gt; Examens { get; set; }
    public DbSet&lt;Subject&gt; Subjects { get; set; }
    public DbSet&lt;Student&gt; Students { get; set; }
    public DbSet&lt;Teacher&gt; Teachers { get; set; }
    public DbSet&lt;TeacherSubject&gt; TeacherSubjects { get; set; }
    public DbSet&lt;Expense&gt; Expenses { get; set; }

    public DbSet&lt;Fee&gt; Fees { get; set; }
    public DbSet&lt;StudentAttendence&gt; StudentAttendences { get; set; }
    public DbSet&lt;TeacherAttendence&gt; TeacherAttendences { get; set; }
}

When I add a migration, I always get this problem :

> Introducing a FOREIGN KEY constraint 'FK_Examens_Subjects_SubjectId' on the 'Exams' table may cause cycles or multiple cascad accesses. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
> Unable to create constraint or index.

I represented my database in Excel :

问题:ASP.NET Core 6 MVC 和 EF Core 中的表之间外键关系

Can someone please help me to find how can I fix this ?

Thanks in advance

答案1

得分: 0

与您的多级级联关系存在问题。最简单的方法是取消ClasseSubject之间的关系:

public class Classe
{
    [Key]
    public int Id { get; set; }
    public string ClassName { get; set; }
    public ICollection<Examen> Examens { get set; }
    //public ICollection<Subject> Subjects { get; set; }
    public ICollection<Student> Students { get; set; }
    public ICollection<TeacherSubject> TeacherSubjects { get; set; }
    public ICollection<Expense> Expenses { get; set; }
    public ICollection<Fee> Fees { get; set; }
    public ICollection<StudentAttendence> StudentAttendence { get; set; }
}

public class Subject
{
    public int Id { get; set; }
    public string SubjectName { get; set; }
    public ICollection<Examen> Examens { get; set; }
    public ICollection<TeacherSubject> TeacherSubjects { get; set; }
    public ICollection<Expense> Expenses { get; set; }
    public ICollection<StudentAttendence> StudentAttendence { get; set; }
    
    //public virtual Classe Classe { get; set; }
    //[ForeignKey(nameof(Classe))]
    //public int ClasseId { get; set; }
}

有关详细信息,您可以参考此链接

英文:

There is a problem with your multi-cascade relationship. The easiest way is to cancel the relationship between Classe and Subject:

public class Classe
{
    [Key]
    public int Id { get; set; }
    public string ClassName { get; set; }
    public ICollection&lt;Examen&gt; Examens { get; set; }
    //public ICollection&lt;Subject&gt; Subjects { get; set; }
    public ICollection&lt;Student&gt; Students { get; set; }
    public ICollection&lt;TeacherSubject&gt; TeacherSubjects { get; set; }
    public ICollection&lt;Expense&gt; Expenses { get; set; }
    public ICollection&lt;Fee&gt; Fees { get; set; }
    public ICollection&lt;StudentAttendence&gt; StudentAttendence { get; set; }
}
public class Subject
{
    public int Id { get; set; }
    public string SubjectName { get; set; }
    public ICollection&lt;Examen&gt; Examens { get; set; }
    public ICollection&lt;TeacherSubject&gt; TeacherSubjects { get; set; }
    public ICollection&lt;Expense&gt; Expenses { get; set; }
    public ICollection&lt;StudentAttendence&gt; StudentAttendence { get; set; }

    //public virtual Classe Classe { get; set; }
    //[ForeignKey(nameof(Classe))]
    //public int ClasseId { get; set; }
}

For details, you can refer to this link.

答案2

得分: 0

以下是您提供的代码的翻译部分:

我找到了解决方案,我在我的 DbContext 类中进行了一些更改。

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
    {

    }

    public DbSet<Classe> Classes { get; set; }
    public DbSet<Examen> Examens { get; set; }
    public DbSet<Subject> Subjects { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Expense> Expenses { get; set; }
    public DbSet<Fee> Fees { get; set; }
    public DbSet<StudentAttendence> StudentAttendences { get; set; }
    public DbSet<TeacherAttendence> TeacherAttendences { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Examen>()
            .HasKey(x => new { x.ClasseId, x.SubjectId });

        modelBuilder.Entity<Examen>()
            .HasOne(x => x.Classe)
            .WithMany(z => z.Examens)
            .HasForeignKey(x => x.ClasseId)
            .OnDelete(DeleteBehavior.ClientSetNull);

        modelBuilder.Entity<Examen>()
            .HasOne(x => x.Subject)
            .WithMany(z => z.Examens)
            .HasForeignKey(x => x.SubjectId)
            .OnDelete(DeleteBehavior.ClientSetNull);

        modelBuilder.Entity<Subject>()
            .HasOne(x => x.Classe)
            .WithMany(z => z.Subjects)
            .HasForeignKey(x => x.ClasseId)
            .OnDelete(DeleteBehavior.ClientSetNull);
    }
}

希望这对您有所帮助。

英文:

I find the solution, I make some changes in my class DbContext

    public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions&lt;SchoolContext&gt; options) : base(options)
    {

    }

    public DbSet&lt;Classe&gt; Classes { get; set; }
    public DbSet&lt;Examen&gt; Examens { get; set; }
    public DbSet&lt;Subject&gt; Subjects { get; set; }
    public DbSet&lt;Student&gt; Students { get; set; }
    public DbSet&lt;Teacher&gt; Teachers { get; set; }
    public DbSet&lt;Expense&gt; Expenses { get; set; }

   public DbSet&lt;Fee&gt; Fees { get; set; }
    public DbSet&lt;StudentAttendence&gt; StudentAttendences { get; set; }
    public DbSet&lt;TeacherAttendence&gt; TeacherAttendences { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity&lt;Examen&gt;().
            HasKey(x=&gt; new {x.ClasseId, x.SubjectId});

        modelBuilder.Entity&lt;Examen&gt;()
            .HasOne(x =&gt; x.Classe)
            .WithMany(z=&gt;z.Examens)
            .HasForeignKey(x=&gt;x.ClasseId)
            .OnDelete(DeleteBehavior.ClientSetNull);

        modelBuilder.Entity&lt;Examen&gt;()
            .HasOne(x =&gt; x.Subject)
            .WithMany(z =&gt; z.Examens)
            .HasForeignKey(x =&gt; x.SubjectId)
            .OnDelete(DeleteBehavior.ClientSetNull);


        modelBuilder.Entity&lt;Subject&gt;()
            .HasOne(x =&gt; x.Classe)
            .WithMany(z =&gt; z.Subjects)
            .HasForeignKey(x =&gt; x.ClasseId)
            .OnDelete(DeleteBehavior.ClientSetNull);


        

    }
}

huangapple
  • 本文由 发表于 2023年2月27日 18:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579132.html
匿名

发表评论

匿名网友

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

确定