Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

api - EF Core Annotation for OnDelete

I hava an annotation problem:

        modelBuilder.Entity<FirstClass>()
            .HasOne(f => f.SecondClass)
            .WithOne(s => s.FirstClass)
            .HasForeignKey<FirstClass>(f => f.SecondClassId)
            .OnDelete(DeleteBehavior.Cascade);

How to write this with annotations? I don't find the annotation for OnDelete.

question from:https://stackoverflow.com/questions/65890729/ef-core-annotation-for-ondelete

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this :

Your model


public class FirstClass
    {
        [Key]
        public int Id { get; set; } 
        public int SecondClassId { get; set; }

        [ForeignKey(nameof(SecondClassId))]
        [InverseProperty("FirstClasses")]
        public virtual SecondClass SecondClass { get; set; }
    }
    public class SecondClass
    {
        [Key]
        public int Id { get; set; }
        [InverseProperty(nameof(FirstClass.SecondClass))]
        public virtual ICollection<FirstClass> FirstClasses { get; set; }

    }

if you want to have only one first and one second try this code. But I don't recommend it since it will be hard to find the errors.

public class FirstClass
    {
        [Key]
        public int Id { get; set; } 
        public int SecondClassId { get; set; }

        [ForeignKey(nameof(SecondClassId))]
        [InverseProperty("FirstClass")]
        public virtual SecondClass SecondClass { get; set; }
    }
    public class SecondClass
    {
        [Key]
        public int Id { get; set; }
        [InverseProperty(nameof(FirstClass.SecondClass))]
        public virtual FirstClass FirstClass { get; set; }

    }

Your db context:


public class FirstClassDbContext : DbContext
    {
        public FirstClassDbContext()
        {
        }

        public FirstClassDbContext(DbContextOptions<FirstClassDbContext> options)
            : base(options)
        {
        }
        
        public DbSet<FirstClass> FirstClasses { get; set; }
        public DbSet<SecondClass> SecondClasses { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            optionsBuilder.UseSqlServer(@"Server=localhost;Database=FirstClass;Trusted_Connection=True;");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<FirstClass>(entity =>
            {
                entity.HasOne(d => d.SecondClass)
                   .WithMany(p => p.FirstClasses)
                   .HasForeignKey(d => d.SecondClassId)
                   .OnDelete(DeleteBehavior.ClientSetNull)
                   .HasConstraintName("FK_FirstClass_SecondClass");
            });

          // or for one to one you can use yours, but I don't recommend it since
//it will be hard to find the errors and it will not do anything for you. 
//Only confusing queries.

             modelBuilder.Entity<FirstClass>()
            .HasOne(f => f.SecondClass)
            .WithOne(s => s.FirstClass)
            .HasForeignKey(d => d.SecondClassId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_FirstClass_SecondClass");
        }
    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...