繁体   English   中英

表拆分实体框架代码优先-3个以上实体

[英]Table Splitting Entity Framework Code First - 3+ Entities

我在程序员中发布了一个问题: https : //softwareengineering.stackexchange.com/questions/315857/entity-framework-code-first-c-class-separation-and-eav

解决该问题的一种方法是在实体框架中进行表拆分。 到目前为止,我已经看到了如何使用2个实体执行此操作,但没有使用3个或更多实体执行此操作。

以下是我要与之共享同一表的模型:

[Table("Tournament")]
    public partial class PGTournament : IImageable
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public GameGenre GameGenre { get; set; }
        public TournamentFormat TournamentFormat { get; set; }

        public TournamentStatus Status { get; set; } 
        public string Description { get; set; }
        public virtual List<PrizePool> Prizes { get; set; }

        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public virtual List<Participants.Participant> Participants { get; set; }
        public decimal Cost { get; set; }
        public string Streaming { get; set; }
        public int? ChallongeTournamentId { get; set; }
        public string Bracket { get; set; }
        public virtual List<TournamentMatch> Matches { get; set; }
        public int MainImageId { get; set; }
        public virtual Media MainImage { get; set; }
        public bool IsFollowUp { get; set; }
        public int? FollowUpTournamentId { get; set; }
        [ForeignKey("FollowUpTournamentId")]
        public virtual PGTournament FollowUptournament { get; set; }
        public int MediaID { get; set; }
        public int MainImageID { get; set; }

        //Properties that share same table:
        public virtual TournamentOrganizer Organizer { get; set; } //Change to Organizer
        public virtual TournamentOrganizerSetting OrganizerSetting { get; set; }
        public virtual TournamentSettings TournamentSettings { get; set; }
        public virtual TournamentRules Rules { get; set; }


    }

您看到的所有虚拟属性均不具有List <>作为其类型,我希望它们共享同一张表(如果可能)。

[Table("Tournament")]
    public partial class TournamentOrganizer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string UserId { get; set; }
        [ForeignKey("UserId")]
        public AppUser User { get; set; }

        public int LogoId { get; set; }
        [ForeignKey("LogoId")]
        public Media Logo { get; set; }
        public virtual TournamentOrganizerSetting Settings { get; set; }
        public virtual TournamentRules Rules { get; set; }
        public virtual TournamentSettings TournamentSettings { get; set; }
        public virtual PGTournament Tournament { get; set; }


    }

[Table("Tournament")]
    public partial class TournamentSettings
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string LocationGoogleMaps { get; set; }
        public bool isOnline { get; set; }
        public int MaxPlayers { get; set; }
        public List<TournamentAssistant> TournamentAssistants { get; set; }

        public virtual TournamentOrganizer Organizer { get; set; } //Change to Organizer
        public virtual TournamentRules Rules { get; set; }
        public virtual TournamentOrganizerSetting OrganizerSettings { get; set; }
        public virtual PGTournament Tournament { get; set; }

    }

 [Table("Tournament")]
    public partial class TournamentOrganizerSetting
    {
        public int Id { get; set; }
        public string Location { get; set; }
        //Properties that share same table:
        public virtual TournamentOrganizer Organizer { get; set; } //Change to Organizer
        public virtual TournamentRules Rules { get; set; }
        public virtual TournamentSettings TournamentSettings { get; set; }
        public virtual PGTournament Tournament { get; set; }
    }

 [Table("Tournament")]
    public partial class TournamentRules
    {
        public int Id { get; set; }
        public string Bans { get; set; }
        public string Allowed { get; set; }
        public string Description { get; set; }
        public string FileName { get; set; }
        public string FilePath { get; set; }

        //Properties that share same table:
        public virtual TournamentOrganizer Organizer { get; set; } //Change to Organizer
        public virtual TournamentOrganizerSetting OrganizerSetting { get; set; }
        public virtual TournamentSettings TournamentSettings { get; set; }
        public virtual PGTournament Tournament { get; set; }
    }

我不知道为什么课程是局部的。 我一直在关注Internet上的一些教程,例如: http : //www.c-sharpcorner.com/UploadFile/ff2f08/table-splitting-in-entity-framework-6-code-first-approach/

我不能让他们工作。

我什至在DbModelBuilder中尝试过:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {    
            modelBuilder.Entity<PGTournament>().ToTable("Tournament");


            modelBuilder.Entity<PGTournament>()
                .HasKey(e => e.Id)
                .HasOptional(e => e.FollowUptournament)
                .WithMany();

            modelBuilder.Entity<PGTournament>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Organizer)
                .WithRequiredDependent(e => e.Organizer)

            modelBuilder.Entity<TournamentOrganizer>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Settings)
                .WithRequiredDependent(e => e.Organizer);

            modelBuilder.Entity<TournamentViewModel>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Settings)
                .WithRequiredDependent(e => e.Organizer);


            modelBuilder.Entity<TournamentOrganizer>().Map(m => m.ToTable("Tournament"));
            modelBuilder.Entity<TournamentOrganizerSetting>().Map(m => m.ToTable("Tournament"));

            base.OnModelCreating(modelBuilder);

        }

似乎没有关于3个或更多实体映射的StackOverflow帖子。

当我尝试运行它时,这是我得到的错误:

One or more validation errors were detected during model generation:

Pro_Gaming.Infrastructure.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Pro_Gaming.Infrastructure.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

此答案来自ASP.NET论坛的Cole Wu: http : //forums.asp.net/p/2093110/6043922.aspx?p=True&t=635968548324560382

答案如下:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Do not delete this:
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Tournament>()
                .HasKey(e => e.TournamentId)
                .HasRequired(e => e.Rules)
                .WithRequiredPrincipal();

            modelBuilder.Entity<Tournament>()
                .HasKey(e => e.TournamentId)
                .HasRequired(e => e.TournamentSettings)
                .WithRequiredDependent();

            modelBuilder.Entity<TournamentOrganizer>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Settings)
                .WithRequiredDependent();


            modelBuilder.Entity<Tournament>().ToTable("Tournament");
            modelBuilder.Entity<TournamentRules>().ToTable("Tournament");
            modelBuilder.Entity<TournamentSettings>().ToTable("Tournament");

            modelBuilder.Entity<TournamentOrganizer>().ToTable("TournamentOrganizer");
            modelBuilder.Entity<TournamentOrganizerSetting>().ToTable("TournamentOrganizer");



        }

解释一下:

  1. 不需要局部类(我之所以这样说是因为有一个示例说明您需要局部类,但事实并非如此):
  2. 我还没有测试过,但是对于要共享同一张表的所有类,我都使用了相同的键。
  3. modelBuilder.Entity <= TheEntity将是您希望所有内容都映射到的主类。
  4. 如果您使用的是ASP.NET Identity,并且是从IdentityDbContext扩展的(这是我的情况),则在OnModelCreating方法中包括base.OnModelCreating(modelBuilder)是非常重要的,否则,Identityissues会给您一个打击,找不到IdenittyUser的主键。

    1. 然后,您将使用:

    modelBuilder.Entity.ToTable(“ MyTable”)modelBuilder.Entity.ToTable(“ MyTable”)modelBuilder.Entity.ToTable(“ MyTable”)

这会将Entity1,Entity2,Entity3等映射到MyTable。

暂无
暂无

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

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