如何使用EF-code-第一大表分成多个离散的类型 [英] How to separate large table into multiple discrete types using EF-Code-First
问题描述
我想一个大表分成多个独立的类型。
我在这里继例如:<一href=\"http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-$c$c-first-part-4-table-splitting.aspx?CommentPosted=true#commentmessage\" rel=\"nofollow\">http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-$c$c-first-part-4-table-splitting.aspx?CommentPosted=true#commentmessage
它的工作用于在主类型和子类型,但是,当我采用多种类型不起作用。我收到一个错误
的实体类型CampaginFeedback'和'CampaignSurvey'不能共享表'运动',因为他们没有诠释,他的同类型层次
或者没有一个有效的与一个外键关系
匹配他们之间的主键。的
块引用>下面是我班的简化版本:
公共类运动{
[键]
公众诠释CAMPAIGNID {获取;设置;}
公共字符串名称{;设置;}
公共虚拟CampaignSurvey调查{获取;设置;}
公共虚拟CampaignFeedback反馈{获取;设置;}
}公共类CampaignSurvey {
[键]
公众诠释CAMPAIGNID {获取;设置;}
公共字符串问题{获取;集;}
公共字符串答{获取;设置;}
}公共类CampaignFeedback {
[键]
公众诠释CAMPAIGNID {获取;设置;}
公共字符串反馈{获取;设置;}
}保护覆盖无效OnModelCreating(DbModelBuilder模型构建器){
modelBuilder.Conventions.Remove&LT; PluralizingTableNameConvention&GT;();
modelBuilder.Entity&LT;&运动GT;()HasRequired(C =&GT; c.Survey)。.WithRequiredPrincipal();
modelBuilder.Entity&LT;&运动GT;()HasRequired(C =&GT; c.Feedback)。.WithRequiredPrincipal();
modelBuilder.Entity&LT;&运动GT;()ToTable(运动)。
modelBuilder.Entity&LT; CampaignSurvey&GT;()ToTable(运动)。
modelBuilder.Entity&LT; CampaignFeedback&GT;()ToTable(运动)。
}
解决方案编辑:分割表在code两个以上的实体首先是很成问题。它的工作原理不使用时EDMX任何问题。
要使它工作,你必须确保用于分割表的每个实体都有有效的与用于分割表中的所有其他实体一到一个关系。这也意味着与导航属性糟蹋你的模型,而且确保在保存指向同一实体类型引用同一实例的所有导航属性(否则你将通话过程中有异常
的SaveChanges
)。因此,对于您的示例解决方案应该是这样的:
公共类运动{
[键]
公众诠释CAMPAIGNID {获取;设置;}
公共字符串名称{;设置;}
公共虚拟CampaignSurvey调查{获取;设置;}
公共虚拟CampaignFeedback反馈{获取;设置;}
}公共类CampaignSurvey {
[键]
公众诠释CAMPAIGNID {获取;设置;}
公共字符串问题{获取;集;}
公共字符串答{获取;设置;} 公共虚拟CampaignFeedback反馈{获取;设置;}
}公共类CampaignFeedback {
[键]
公众诠释CAMPAIGNID {获取;设置;}
公共字符串反馈{获取;设置;}
}保护覆盖无效OnModelCreating(DbModelBuilder模型构建器){
modelBuilder.Conventions.Remove&LT; PluralizingTableNameConvention&GT;(); modelBuilder.Entity&LT;&运动GT;()HasRequired(C =&GT; c.Survey)。.WithRequiredPrincipal();
modelBuilder.Entity&LT;&运动GT;()HasRequired(C =&GT; c.Feedback)。.WithRequiredPrincipal();
modelBuilder.Entity&LT; CampaignSurvey&GT;()HasRequired(C =&GT; c.Feedback)。.WithRequiredPrincipal(); modelBuilder.Entity&LT;&运动GT;()ToTable(运动)。
modelBuilder.Entity&LT; CampaignSurvey&GT;()ToTable(运动)。
modelBuilder.Entity&LT; CampaignFeedback&GT;()ToTable(运动)。
}我甚至不知道这将如何在现实场景中工作。你可以使用它的时候发现一些其他问题。
I am trying to separate a large table into multiple discrete types.
I'm following the example here: http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx?CommentPosted=true#commentmessage
It's working for a primary type and a sub-type, but does not work when I employ multiple types. I received an error
The Entity types 'CampaginFeedback' and 'CampaignSurvey' cannot share table 'Campaign' because they are not int he same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.
Here are simplified versions of my classes:
public class Campaign { [Key] public int CampaignId {get;set;} public string Name {get;set;} public virtual CampaignSurvey Survey {get;set;} public virtual CampaignFeedback Feedback {get;set;} } public class CampaignSurvey { [Key] public int CampaignId {get;set;} public string Question {get;set;} public string Answer {get;set;} } public class CampaignFeedback { [Key] public int CampaignId {get;set;} public string Feedback {get;set;} } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Campaign>().HasRequired(c => c.Survey).WithRequiredPrincipal(); modelBuilder.Entity<Campaign>().HasRequired(c => c.Feedback).WithRequiredPrincipal(); modelBuilder.Entity<Campaign>().ToTable("Campaign"); modelBuilder.Entity<CampaignSurvey>().ToTable("Campaign"); modelBuilder.Entity<CampaignFeedback>().ToTable("Campaign"); }
解决方案Edit: Split table to more than two entities in code first is very problematic. It works without any problem when using EDMX.
To make it work you must ensure that each entity used to split table has valid one-to-one relation with all other entities used to split table. That also means spoiling your model with navigation properties and moreover ensuring that during save all navigation properties pointing to the same entity type reference the same instance (otherwise you will get exception during call to
SaveChanges
).So the solution for your example should be something like:
public class Campaign { [Key] public int CampaignId {get;set;} public string Name {get;set;} public virtual CampaignSurvey Survey {get;set;} public virtual CampaignFeedback Feedback {get;set;} } public class CampaignSurvey { [Key] public int CampaignId {get;set;} public string Question {get;set;} public string Answer {get;set;} public virtual CampaignFeedback Feedback {get;set;} } public class CampaignFeedback { [Key] public int CampaignId {get;set;} public string Feedback {get;set;} } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Campaign>().HasRequired(c => c.Survey).WithRequiredPrincipal(); modelBuilder.Entity<Campaign>().HasRequired(c => c.Feedback).WithRequiredPrincipal(); modelBuilder.Entity<CampaignSurvey>().HasRequired(c => c.Feedback).WithRequiredPrincipal(); modelBuilder.Entity<Campaign>().ToTable("Campaign"); modelBuilder.Entity<CampaignSurvey>().ToTable("Campaign"); modelBuilder.Entity<CampaignFeedback>().ToTable("Campaign"); }
I'm even not sure how this will work in the real scenario. You can find some other problems when using it.
这篇关于如何使用EF-code-第一大表分成多个离散的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!