我在程序员中发布了一个问题: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&lt;&gt;作为他们的类型,我希望他们共享一个相同的表(如果可能的话)。
[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; }
}
我不知道为什么这些课程是偏袒的。我一直关注互联网上的几个教程,例如: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);
}
似乎没有StackOverflow帖子映射到3个或更多实体。
当我尝试运行它时,这是我得到的错误:
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.
答案 0 :(得分:0)
这个答案来自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");
}
解释一下:
如果您正在使用ASP.NET身份,并且您正在从IdentityDbContext扩展(这是我的情况),这非常重要 在OnModelCreating方法中包含base.OnModelCreating(modelBuilder),否则你将被Identityissues命中,因为它没有找到IdenittyUser的主键。
modelBuilder.Entity.ToTable(&#34; MyTable的&#34) modelBuilder.Entity.ToTable(&#34; MyTable的&#34) modelBuilder.Entity.ToTable(&#34; MyTable的&#34)
这会将Entity1,Entity2,Entity3等映射到MyTable。