EF Core:Cascade Delete支持多对多关系

时间:2019-11-16 16:17:07

标签: c# .net entity-framework entity-framework-core

我正在使用EF Core(代码优先)进行.NET Core项目。我正在构建一个API来学习此框架。

总而言之,该应用程序将使用户能够粘贴其代码并与其他用户共享。 所以我有2个主要实体:

  • 用户
  • 列表项

CodePage

 public sealed class User : Entity<Guid>
    {
        /// <summary>
        /// Le nom d'utilisateur de l'utilisateur
        /// </summary>
        public string Username { get; private set; }

        /// <summary>
        /// Le hash du mot de passe de l'utilisateur
        /// </summary>
        public byte[] PasswordHash { get; private set; }

        /// <summary>
        /// Le salt du mot de passe de l'utilisateur
        /// </summary>
        public byte[] PasswordSalt { get; private set; }

        /// <summary>
        /// Liste de pages de code dont l'utilisateur est proprietaire
        /// </summary>
        public ICollection<CodePage> OwnedCodePages { get; private set; }

        /// <summary>
        /// Association User/Page autorisées
        /// </summary>
        public ICollection<UserCodePage> UserCodePages { get; private set; }
    ...
}

public sealed class CodePage : Entity<Guid>
    {
        /// <summary>
        /// Titre optionnel de la page de code
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// Indication du langage de la page de code
        /// </summary>
        public string Language { get; private set; }

        /// <summary>
        /// Contenu de la page de code
        /// </summary>
        public string Content { get; private set; }

        /// <summary>
        /// Date de création
        /// </summary>
        public DateTime CreatedAt { get; private set; }

        /// <summary>
        /// Date de modification
        /// </summary>
        public DateTime UpdatedAt { get; private set; }

        /// <summary>
        /// Flag indiquant si il s'agit d'une page de code privée
        /// </summary>
        public bool IsPrivate { get; private set; }

        /// <summary>
        /// Propriétaire de la page de code (Anonyme si null)
        /// </summary>
        public User Owner { get; private set; }

        /// <summary>
        /// Association User/Page autorisées
        /// </summary>
        public IEnumerable<UserCodePage> UserCodePages { get; set; }
...
}

我想要的是用户和代码页之间的多对多关系。 因此,我关注了文档(https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration)。我有这个:

public class UserCodePage : BaseEntity
    {

        public Guid AuthorizedUserId { get; private set; }
        public User AuthorizedUser { get; set; }
        public Guid CodePageId { get; private set; }
        public CodePage CodePage { get; set; }

        public UserCodePage()
        {
        }

        public UserCodePage(Guid userId, User user, Guid codePageId, CodePage codePage)
        {
            AuthorizedUserId = userId;
            AuthorizedUser = user;
            CodePageId = codePageId;
            CodePage = codePage;
        }
    }

实体配置:

internal class UserCodePage : BaseEntityTypeConfiguration<Identity.Entities.UserCodePage>
    {
        public override void Configure(EntityTypeBuilder<Identity.Entities.UserCodePage> builder)
        {
            builder.HasKey(o => new { o.AuthorizedUserId, o.CodePageId });

            builder
                .HasOne(o => o.AuthorizedUser)
                .WithMany(o => o.UserCodePages)
                .HasForeignKey(o => o.AuthorizedUserId)
                .OnDelete(DeleteBehavior.Restrict);



            builder
                .HasOne(o => o.CodePage)
                .WithMany(o => o.UserCodePages)
                .HasForeignKey(o => o.CodePageId)
                .OnDelete(DeleteBehavior.Restrict);

        }

我添加了OnDelete(DeleteBehavior.Restrict),因为没有它我无法Update Database

这是问题所在: 如果要删除与之链接的UserCodePageUser,我想删除CodePage。 我尝试使用DeleteBehavior.Cascade,但Update-Database失败...

希望您能理解我的问题!

0 个答案:

没有答案