简体   繁体   English

实体框架:一个具有多个多对多关系的实体

[英]Entity Framework : one entity with multiple many to many relationship

I'm doing a "manga website" with ASP.NET Core and Entity Framework and Razor pages.我正在做一个带有 ASP.NET 核心和实体框架以及 Razor 页面的“漫画网站”。 What I'm trying to do is having my MangaModul become MangaAuthorArtistGroupScanlationChapterPost Module to speak with multiple many to many modules in the database,for when i want to retrieve information.我想要做的是让我的MangaModul成为MangaAuthorArtistGroupScanlationChapterPost Module ,以便在我想检索信息时与数据库中的多个多对多模块对话。

An example would be of:一个例子是:

  • a one entity many to many would be Manga Module / Author Module = MangaAuthor Module一个多对多的实体是 Manga Module / Author Module = MangaAuthor Module

  • My Goal is to have a Joint table of all the ICollections MangaAuthorArtistGroupScanlationChapterPost我的目标是拥有所有 ICollections MangaAuthorArtistGroupScanlationChapterPost的联合表

I am fully aware that I have not made a我完全清楚我没有制作

protected override void OnModelCreating(ModelBuilder modelBuilder)

I tried using EF6s relationship builder, To fix a MangaAuthorArtist~Modul in the database, however it did not work.我尝试使用 EF6s 关系构建器来修复数据库中的MangaAuthorArtist~Modul ,但是它不起作用。

My first guess is that the complexity of relationship isn't doable with automatic EF with these many relationship binding, and I need to write them myself.我的第一个猜测是,具有这么多关系绑定的自动 EF 无法实现关系的复杂性,我需要自己编写它们。

2nd guess is that somewhere I'm doing a big mistake in the module where I construct them.第二个猜测是我在构建它们的模块中犯了一个大错误。

I have read我读过了

and several others, but my findings are often same as it's a single many to many relationship (which I already know how to solve)和其他几个,但我的发现通常与它是单一的多对多关系相同(我已经知道如何解决)

public class AuthorModel
{
    [Key]
    public int AuthorId { get; set; }

    ~Info~

    ICollection<MangaModel> Models { get; set; }
}

public class ArtistModel
{
    [Key]
    public int ArtistId { get; set; }
    ~Info ~
    ICollection<MangaModel> Models { get; set; }
}

 public class PostModel
 {
    [Key]
    public int PostId { get; set; }

    public int MangaId { get; set; }
    public MangaModel mangaModel { get; set; }
}

public class StudioModel
{
    [Key]
    public int StudioId { get; set; }
    Info
}

public class PostModel
{
    [Key]
    public int PostId { get; set; }
    Info
    public int MangaId { get; set; }
    public MangaModel mangaModel { get; set; }
}

public enum GenresModel
{
     None,
     Action,
     Adult,
     Adventure, 
}

public enum ThemeModel
{       
    None,
    Action,
    Adult,
    Adventure,
}

public class ChapterModel
{
    [Key]
    public int chapterID { get; set; }

    public int MangaId { get; set; }
    public MangaModel mangaModel { get; set; }
}

public class MangaModel
{
    [Key]
    public int MangaID { get; set; }

    [Required(ErrorMessage = "Name is required"),
    MinLength(2, ErrorMessage = "Name must contain at least 2 characters")]
    public string MangaName { get; set; }
         ~Info~
    public GenresModel? Genres { get; set; }
    public ThemeModel? Theme { get; set; }
    ICollection<StudioModel> StudioModels { get; set; }
    ICollection<ArtistModel> ArtistModels { get; set; }
    ICollection<AuthorModel> AuthorModels { get; set; }
    ICollection<ChapterModel> ChaptersModels { get; set; }
    ICollection<PostModel> Posts { get; set; }
    ICollection<GroupScanlatingModel> GroupScanlatingModels { get; set; }
}

public class MangaNNovelAuthDBContext : IdentityDbContext
{
    public MangaNNovelAuthDBContext(DbContextOptions<MangaNNovelAuthDBContext> options) :  base(options)
    {
    }

    public DbSet<ArtistModel> artistModels { get; set; }
    public DbSet<AuthorModel> authorModels { get; set; }
    public DbSet<ChapterModel> chapterModels { get; set; }

    public DbSet<GroupScanlatingModel> groupScanlatingModels { get; set; }

    public DbSet<StudioModel> studioModels { get; set; }
    public DbSet<PostModel> Posts { get; set; }
    public DbSet<MangaModel> mangaModels { get; set; }
}

A photo of the migrated database:迁移数据库的照片:

"Edited Wrong picture from another DB, but same problem. Correct picture is now in place." “从另一个数据库编辑了错误的图片,但同样的问题。正确的图片现在就位。” [:[A Photo of the database](https.//i.stack.imgur.com/2ZxhO.png)] [:[数据库照片](https.//i.stack.imgur.com/2ZxhO.png)]

I have tried creating my own relationship paths with OnModelCreating(ModelBuilder modelBuilder) , but it does not give any result.我尝试使用OnModelCreating(ModelBuilder modelBuilder)创建自己的关系路径,但它没有给出任何结果。

I have succeeded with a previous work, but the modules were only 2. so It was a single Many to many relationship and not Multiple many to many relationship where several modules speak with the same module我已经成功完成了之前的工作,但模块只有 2 个。所以这是一个多对多关系,而不是多个多对多关系,其中多个模块与同一个模块说话

Book and Author, where EF successfully created BookAuthor. Book 和 Author,EF 成功创建 BookAuthor 的地方。

My goal or wish was to have Entity framework build the relationship without fluent API.我的目标或希望是让实体框架在没有流畅的 API 的情况下建立关系。 I am not sure if this will be a "great solve" but i believe this might be a solution to the problem.我不确定这是否是一个“很好的解决方案”,但我相信这可能是解决问题的方法。 haven't seen yet what it might devolve into.还没有看到它可能会演变成什么。

I'm still new with EF6, if anyone can give me a better solution or a tip on how to make it easier to control or fewer risk of having larger problems at larger databases.我还是 EF6 的新手,如果有人能给我一个更好的解决方案或关于如何更容易控制或降低在大型数据库中出现更大问题的风险的提示。

but Here's the solution但这是解决方案
I created a another Module called Master Module;我创建了另一个名为 Master Module 的模块; See code below.请参阅下面的代码。 A photo of a Migrated and Updated database已迁移和更新的数据库的照片

         protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

             modelBuilder.Entity<MasterModel>()
            .HasKey(m => new
            {
                m.MangaModelId,
                m.GroupsSanlatingId,
                m.postModelID,
                m.ChapterModelId,
                m.ArtistModelID,
                m.AuthorModelID,
                m.StudioModelID
            });

             modelBuilder.Entity<MasterModel>()
            .HasOne(mm => mm.mangaModel)
            .WithMany(m => m.MasterModels)
            .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);
        modelBuilder.Entity<MasterModel>()
            .HasOne(mm => mm.ArtistModel)
            .WithMany(m => m.MasterModels)
            .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);
        modelBuilder.Entity<MasterModel>()
           .HasOne(mm => mm.GroupScanlatingModel)
           .WithMany(m => m.MasterModels)
           .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);

            modelBuilder.Entity<MasterModel>()
           .HasOne(mm => mm.StudioModel)
           .WithMany(m => m.MasterModels)
           .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);

        modelBuilder.Entity<MasterModel>()
           .HasOne(mm => mm.chapterModel)
           .WithMany(m => m.MasterModels)
           .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);

        modelBuilder.Entity<MasterModel>()
           .HasOne(mm => mm.PostModel)
           .WithMany(m => m.MasterModels)
           .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);

        modelBuilder.Entity<MasterModel>()
           .HasOne(mm => mm.AuthorModel)
           .WithMany(m => m.MasterModels)
           .HasForeignKey(mm => mm.MangaModelId).OnDelete(DeleteBehavior.ClientCascade);

        modelBuilder.Entity<ChapterModel>()
            .HasOne(cm => cm.GroupScanlatingModel)
            .WithMany(c => c.chapterModels)
            .HasForeignKey(pt => pt.chapterID).OnDelete(DeleteBehavior.ClientCascade); ;
        base.OnModelCreating(modelBuilder);

         }
          public class MasterModel
{
    [Key]
    public int MasterID { get; set; }
    [Key]
    public int postModelID { get; set; }
    public PostModel PostModel { get; set; }

    [Key]
    public int StudioModelID { get; set; }
    public StudioModel StudioModel { get; set; }
    [Key]
    public int GroupsSanlatingId { get; set; }
    public GroupScanlatingModel GroupScanlatingModel { get; set; }
    [Key]
    public int ChapterModelId { get; set; }
    public ChapterModel chapterModel { get; set; }
    [Key]
    public int AuthorModelID { get; set; }
    public AuthorModel AuthorModel { get; set; }
    [Key]
    public int ArtistModelID { get; set; }
    public ArtistModel ArtistModel { get; set; }
    [Key]
    public int MangaModelId { get; set; }
    public MangaModel mangaModel { get; set; }
}

Saving previous statement if someone might think it's better than this.如果有人认为它比这更好,则保存先前的语句。 Previous answer overflows the database and creates a lot of copies.以前的答案会溢出数据库并创建大量副本。 See picture Old Database photo link If the MasterModel get's an input from ChapterModel, ex chapter 2, it creates a new colum, with previous statement.参见图片旧数据库图片链接如果 MasterModel 从第 2 章中的 ChapterModel 获取输入,它会创建一个新列,其中包含先前的语句。 A manga can have hundreds of chapters.一部漫画可以有数百章。 If i understand right.如果我理解正确。 The way it's set right now, it will make copies for every previous colum, if there's 4 unique artist.现在设置的方式,如果有 4 个独特的艺术家,它将为之前的每一列制作副本。 there will be atleast 4x amount of chapters.将有至少 4 倍数量的章节。 (unsure if this is true) (不确定这是否属实)

But from my understanding this will become messy and fast.但据我了解,这将变得混乱而快速。

This new Answer i'm doing creates a more normal looking many to many, where the database will get "messy" however, the colums of MasterModel with author will only update on the specific location instead of all Columns that touches artist/author/group etc.我正在做的这个新答案创建了一个看起来更正常的多对多,其中数据库将变得“混乱”但是,带有作者的 MasterModel 的列只会在特定位置更新,而不是所有触及艺术家/作者/组的列等等
Note: I'm calling blog in the flowchart for Forum.注意:我在论坛的流程图中调用博客。 I created a flowchart for how i built my Classes.我为我如何构建我的类创建了一个流程图。 Hopefully this will make it easier on how i was thinking FlowChart希望这会让我更容易理解流程图

New Database Photo新数据库照片

     public class MangaNNovelAuthDBContext : IdentityDbContext
      {
        public MangaNNovelAuthDBContext(DbContextOptions<MangaNNovelAuthDBContext> options) : base(options)
    {

    }
    public DbSet<ArtistModel> artistModels { get; set; }
    public DbSet<AuthorModel> authorModels { get; set; }
    public DbSet<ChapterModel> chapterModels { get; set; }

    public DbSet<GroupScanlatingModel> groupScanlatingModels { get; set; }
    public DbSet<UserModel>UserModels { get; set;  }
    public DbSet<StudioModel> studioModels { get; set; }
    public DbSet<PostModel> PostModels { get; set; }
    public DbSet<BlogModel> blogModels { get; set; }
    public DbSet<MangaModel> mangaModels { get; set; }
    public DbSet<MasterModel> MasterModels { get; set; }
     protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
        modelBuilder.Entity<MasterModel>()
       .HasOne(mm => mm.MangaModels)
       .WithOne(i => i.MasterModels)
       .HasForeignKey<MangaModel>(b => b.MangaID);
        base.OnModelCreating(modelBuilder);
        }
    }
    public class ArtistModel : UserModel
      {
          No navigation needed usermodel does it for us and AuthorModel is a replica.
       }

           public class StudioModel : GroupScanlatingModel
          {

                 //Info
             }

        public class BlogModel
    {
    [Key]
    public int id { get; set; }
    public string mangaName { get; set; }
    public ICollection<PostModel> postsModel { get; set; }
    public int mangaId { get; set; }
    public MangaModel MangaModel { get; set; }
    }

           public class PostModel
     {
    [Key]
    public int PostId { get; set; }

    public string Title { get; set; }
    public string postComment { set; get; }

    public int BlogId { get; set; }
    public BlogModel BlogModel { get; set; }

    public string UserID { get; set; }
    public virtual UserModel UserModel { get; set; }
        }

           public class ChapterModel
      {
    [Key]
    public int chapterID { get; set; }
       //Info Dump skippped
 

    public int GroupScanlatingID { get; set; }
    public GroupScanlatingModel GroupScanlatingModels { get; set; }   
      }

           public class GenresModel
         {
    [Key]
    public int GenresId { get; set; }
   // Genres and Tag are replicas will skip Tag
    public int MangaID { get; set; }
    public MangaModel mangaModel { get; set; }
          }


            public class GroupScanlatingModel
       {
    [Key]
    public int GroupScanlatingID { get; set; }
    [Required]
    public string GroupName { get; set; }
    public string? website { get; set; }
    public ICollection<ChapterModel> chapterModels { get; set; }
    public ICollection<MasterModel> MasterModels { get; set; }

    public ICollection<UserModel> userModels { get; set; }
 
      }

          public class MangaModel
         {
    [Key]
    public int MangaID { get; set; }

    [Required(ErrorMessage = "Name is requried"),
    MinLength(2, ErrorMessage = "Name must contain at least 2 characters")]

    public string MangaName { get; set; }
         
    public int BlogModelID { get; set; }
    public BlogModel BlogModel { get; set; }

    public int MasterModelID { get; set; }
    public MasterModel MasterModels { get; set; }
    public ICollection<GenresModel> GenresModels { get; set; }
    public ICollection<TagModel> TagsModels { get; set; }

            }
           public class MasterModel
{
    [Key]
    public int MasterID { get; set; }

    public ICollection<GroupScanlatingModel> GroupScanlating { get; set; }
    public ICollection<UserModel> userModels { get; set; }

    [Required]
    public MangaModel MangaModels { get; set; }
                }

          public class UserModel : IdentityUser
        {

    public string Allias { get; set; }
    public string ForumName { get; set; }
    //navigation 
    public ICollection<GroupScanlatingModel> GroupScanlating { get; set; }
    public ICollection<MasterModel> MasterModel { get; set; }
    public ICollection<PostModel> PostModel { get; set; }
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM