EF sqlite数据库表的级联删除

au9on6nz  于 2023-11-21  发布在  SQLite
关注(0)|答案(2)|浏览(254)

有人可以帮助我写正确的代码DeleteBehavior.ClientCascade请.我有两个类AccountSchedule定义如下(Account可以有许多Schedule对象):

public class Account : IdentityUser
        { 
            public string Title { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
    
            public List<Schedule> Schedules { get; set; }
        }
        public class Schedule
        {
                public string AccountAccountId { get; set; }
                public int ScheduleId { get; set; }
                public DateTime Date { get; set; }
        }

... and the context

     public class DataContext : IdentityDbContext<Account>
        {
            public DbSet<Account> Accounts { get; set; }
            public DbSet<Schedule> Schedules { get; set; }
            public DataContext(IConfiguration configuration, DbContextOptions options) : base(options)
            {
                Configuration = configuration;
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder options)
            {
                // connect to sqlite database
                options.UseSqlite(Configuration.GetConnectionString("WebApiDatabase"));
            }
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                //modelBuilder.Entity<Account>().HasMany(e => e.RefreshTokens).WithOne(e => e.Account).IsRequired();
    
                modelBuilder.Entity<Schedule>()
                .HasOne(b => b.Account)
                .HasMany(a => a.Schedules) <--- Error
                .OnDelete(DeleteBehavior.ClientCascade); <--- Error
            }
        }

字符串
我正在关注这篇文章link,但在尝试执行以下操作时,我的DataContext出现错误:

modelBuilder.Entity<Schedule>()
        .HasOne(b => b.Account)<--- error
        .HasMany(a => a.Schedules) 
        .OnDelete(DeleteBehavior.ClientCascade);

knpiaxh1

knpiaxh11#

此文档不完整。也许您应该先尝试此https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration。要定义一对多关系。您需要像下面这样创建Schedule类

public class Schedule
    {
        public string AccountAccountId { get; set; }
        public int ScheduleId { get; set; }
        public DateTime Date { get; set; }
        public Account? Account { get;set; }
    }

字符串
在这里使用Account?使foreignkey AccountId可为空。
通常使用“s”代表“schedule”,并且您的schedule类现在有一个“Account”字段。

modelBuilder.Entity<Schedule>()
                .HasOne(s => s.Account)
                .WithMany(a => a.Schedules);


modelBuilder.Entity<Account()
                .HasMany(a => a.Schedules)
                .WithOne(s => s.Account);


它们有相同的结果。必须首先使用Has..作为实体的后面,然后使用With。您的代码实际上使用了2 Has...方法。
如果没有将?添加到Account?以使其为空,则.OnDelete(DeleteBehavior.ClientCascade);可能会出错。

nhaq1z21

nhaq1z212#

这对我来说是有效的,无需修改Schedule类:

modelBuilder.Entity<Account>()
                .HasMany<Schedule>(a => a.Schedules)
                .WithOne()
                .OnDelete(DeleteBehavior.ClientCascade);

字符串

相关问题