当前位置:  开发笔记 > 编程语言 > 正文

ADO EF - 错误映射TPH中派生类型之间的关联

如何解决《ADOEF-错误映射TPH中派生类型之间的关联》经验,为你挑选了1个好方法。



1> Zach Burling..:

可能的解决方法

    为派生类型之间的每个关联创建一个单独的列,并使每个列都可以为

    在每个新列和主键表之间创建一个外键.

    将实体模型中的每个关联映射到特定的唯一列和外键,以便每个列和外键仅使用一次.


问题

这是一个非常不合适的解决方案,因为它会爆炸出您需要的列数.

更多列 - 为派生类型之间的每个关联添加列会导致列数爆炸.

空列在TPH的情况下,这意味着您的表中会有很多列.

SQL JOIN - Switching from TPH to TPT to avoid the number of empty columns results in the necessity for EF to use a JOIN which will have to occur extremely frequently (almost every time you deal with any of the derived types).

Refactoring If you add a derived type in the future, you not only have to update your Entity model (*.edmx) and the it's mapping but you will also have to change the database schema by adding additional columns!


Example

For the Link/Node example above, the resulting database schema would look like this:

GraphExample Workaround Database Schema http://img230.imageshack.us/img230/1628/graphexampledatabasewor.th.png


Code

SQL:

USE [GraphExample2]
GO
/****** Object:  Table [dbo].[Node]    Script Date: 02/26/2009 15:45:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Node](
    [NodeID] [int] IDENTITY(1,1) NOT NULL,
    [NodeTypeDiscriminator] [int] NOT NULL,
    [Name] [varchar](255) NOT NULL,
    [Description] [varchar](1023) NULL,
 CONSTRAINT [PK_Node] PRIMARY KEY CLUSTERED 
(
    [NodeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Link]    Script Date: 02/26/2009 15:45:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Link](
    [LinkID] [int] IDENTITY(1,1) NOT NULL,
    [LinkTypeDiscriminator] [int] NOT NULL,
    [LeaderID] [int] NULL,
    [FollowerID] [int] NULL,
    [PersonID] [int] NULL,
    [LocationID] [int] NULL,
    [Name] [varchar](255) NULL,
    [Description] [varchar](1023) NULL,
 CONSTRAINT [PK_Link] PRIMARY KEY CLUSTERED 
(
    [LinkID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  ForeignKey [FK_Link_Node_Follower]    Script Date: 02/26/2009 15:45:53 ******/
ALTER TABLE [dbo].[Link]  WITH CHECK ADD  CONSTRAINT [FK_Link_Node_Follower] FOREIGN KEY([FollowerID])
REFERENCES [dbo].[Node] ([NodeID])
GO
ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Follower]
GO
/****** Object:  ForeignKey [FK_Link_Node_Leader]    Script Date: 02/26/2009 15:45:53 ******/
ALTER TABLE [dbo].[Link]  WITH CHECK ADD  CONSTRAINT [FK_Link_Node_Leader] FOREIGN KEY([LeaderID])
REFERENCES [dbo].[Node] ([NodeID])
GO
ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Leader]
GO
/****** Object:  ForeignKey [FK_Link_Node_Location]    Script Date: 02/26/2009 15:45:53 ******/
ALTER TABLE [dbo].[Link]  WITH CHECK ADD  CONSTRAINT [FK_Link_Node_Location] FOREIGN KEY([LocationID])
REFERENCES [dbo].[Node] ([NodeID])
GO
ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Location]
GO
/****** Object:  ForeignKey [FK_Link_Node_Person]    Script Date: 02/26/2009 15:45:53 ******/
ALTER TABLE [dbo].[Link]  WITH CHECK ADD  CONSTRAINT [FK_Link_Node_Person] FOREIGN KEY([PersonID])
REFERENCES [dbo].[Node] ([NodeID])
GO
ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Person]
GO

EDMX:


  
    
    
      
      
        
          
            
            
            
              
              
            
            
              
              
            
            
              
              
            
            
              
              
            
          
          
            
              
            
            
            
            
            
            
            
            
            
          
          
            
              
            
            
            
            
            
          
          
            
            
            
              
                
              
              
                
              
            
          
          
            
            
            
              
                
              
              
                
              
            
          
          
            
            
            
              
                
              
              
                
              
            
          
          
            
            
            
              
                
              
              
                
              
            
          
        
      
      
      
        
          
            
            
            
              
              
            
            
              
              
            
            
              
              
            
            
              
              
            
          
          
            
              
            
            
            
            
          
          
            
            
            
          
          
            
          
          
            
              
            
            
            
            
          
          
            
            
          
          
            
            
          
          
            
            
          
          
            
            
          
          
            
            
          
          
            
            
          
        
      
      
      
        
          
          
          
            
              
                
                  
                  
                  
                
              
              
                
                  
                  
                
              
              
                
                  
                  
                
              
            
            
              
                
                  
                  
                  
                
              
              
                
                  
                  
                
              
              
                
                  
                  
                
              
            
            
              
                
              
              
                
              
            
            
              
                
              
              
                
              
            
            
              
                
              
              
                
              
            
            
              
                
              
              
                
              
            
          
        
      
    
    
    
      
        
          
        
      
      
        
          
        
      
      
      
        
          
          
          
          
            
            
            
          
          
          
          
            
            
          
          
            
            
          
          
          
            
            
            
          
          
            
            
          
          
            
            
          
          
            
            
            
          
          
            
            
          
        
      
    
  

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有