简体   繁体   English

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

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

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

One solution to the problem is Table Splitting in Entity Framework. 解决该问题的一种方法是在实体框架中进行表拆分。 So far, I've seen how to do this with 2 entities, but not with 3 or more. 到目前为止,我已经看到了如何使用2个实体执行此操作,但没有使用3个或更多实体执行此操作。

Here are the models I want to share a same table with: 以下是我要与之共享同一表的模型:

[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; }


    }

All the properties you see that are virtual and don't have a List<> as their type, I want them to share a same table (If it is possible). 您看到的所有虚拟属性均不具有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; }
    }

I don't know why the classes are partial. 我不知道为什么课程是局部的。 I've been following several tutorials over the Internet, such as this: http://www.c-sharpcorner.com/UploadFile/ff2f08/table-splitting-in-entity-framework-6-code-first-approach/ 我一直在关注Internet上的一些教程,例如: http : //www.c-sharpcorner.com/UploadFile/ff2f08/table-splitting-in-entity-framework-6-code-first-approach/

I can't get them to work. 我不能让他们工作。

I have even tried this in the DbModelBuilder: 我什至在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);

        }

There doesn't seem to be a StackOverflow post with Mapping to 3 or more entities. 似乎没有关于3个或更多实体映射的StackOverflow帖子。

When I try to run it, this is the error I get: 当我尝试运行它时,这是我得到的错误:

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.

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

The answer is the following: 答案如下:

 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");



        }

Explaining a bit: 解释一下:

  1. There is no need for partial classes (I say this because there is an example that states that you need partial classes, this is not true): 不需要局部类(我之所以这样说是因为有一个示例说明您需要局部类,但事实并非如此):
  2. I haven't tested this, but I used the same key for all the classes that I wanted to share the same table. 我还没有测试过,但是对于要共享同一张表的所有类,我都使用了相同的键。
  3. modelBuilder.Entity <= TheEntity will be the main class you want everything mapped to. modelBuilder.Entity <= TheEntity将是您希望所有内容都映射到的主类。
  4. If you are using ASP.NET Identity and you are extending from IdentityDbContext (which is my case), It is very important to include base.OnModelCreating(modelBuilder) in the OnModelCreating method, otherwise you'll be hit with Identityissues that it doesn't find the primary key for IdenittyUser. 如果您使用的是ASP.NET Identity,并且是从IdentityDbContext扩展的(这是我的情况),则在OnModelCreating方法中包括base.OnModelCreating(modelBuilder)是非常重要的,否则,Identityissues会给您一个打击,找不到IdenittyUser的主键。

    1. You would then use: 然后,您将使用:

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

This will map Entity1, Entity2, Entity3, etc to MyTable. 这会将Entity1,Entity2,Entity3等映射到MyTable。

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

相关问题 首先使用Entity Framework代码将2个相关实体合并到1个表中 - 2 related entities into 1 table using Entity Framework code first 实体框架代码优先-相同类型的2个实体 - Entity Framework Code First - 2 Entities of the Same Type 实体框架代码优先和链接实体 - Entity Framework Code First and linked entities 实体框架-代码优先-没有共享主键时的实体拆分 - Entity Framework - Code First - Entity Splitting When There Is No Shared Primary Key 实体框架 - 代码优先 - 允许多个实体引用单个实体 - Entity Framework - Code First - Allowing Multiple Entities to reference a single entity 在实体框架中使用来自不同实体的相同数据库表-代码优先 - Use same db table from different entities in Entity Framework- Code first 为什么我的实体框架代码不首先DbContext显示我填充的表中的实体? - Why doesn't my Entity Framework code first DbContext show the entities from my populated table? 使用分离的实体首先在Entity Framework 5中使用代码进行更新 - Updating using code first in Entity Framework 5 with detached entities 实体框架代码优先关系:一对多到多个实体 - Entity Framework Code First Relationships: One to Many to Multiple Entities 实体框架代码优先 - 两个具有相同名称但位于不同名称空间的实体 - Entity Framework Code First - two entities with same name but in different namespaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM