ADO EF - Errors Mapping Associations between Derived Types in TPH

后端 未结 3 1563
梦毁少年i
梦毁少年i 2021-02-08 01:09

Background

I am writing a data access library using the ADO Entity Framework in Visual Studio 2008 SP1 using the .NET Framework 3.5 SP1. I am trying to create associat

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 01:57

    Possible Workaround

    1. Create a separate column for each association between derived types and make each of these columns nullable
    2. Create a foreign key between each of these new columns and the primary key table.
    3. Map each association in your Entity Model to a specific, unique column and foreign key so that each column and foreign key is only used once.

    Problems

    This is quite an undesirable solution because it explodes out the number of columns you need.

    • More Columns - Adding a column for each association between derived types results in an explosion on the number of columns.
    • Empty Columns In the case of TPH, it means you'll have a lot of empty columns in your table.
    • 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:


    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:

    
      
        
        
          
          
            
              
                
                
                
                  
                  
                
                
                  
                  
                
                
                  
                  
                
                
                  
                  
                
              
              
                
                  
                
                
                
                
                
                
                
                
                
              
              
                
                  
                
                
                
                
                
              
              
                
                
                
                  
                    
                  
                  
                    
                  
                
              
              
                
                
                
                  
                    
                  
                  
                    
                  
                
              
              
                
                
                
                  
                    
                  
                  
                    
                  
                
              
              
                
                
                
                  
                    
                  
                  
                    
                  
                
              
            
          
          
          
            
              
                
                
                
                  
                  
                
                
                  
                  
                
                
                  
                  
                
                
                  
                  
                
              
              
                
                  
                
                
                
                
              
              
                
                
                
              
              
                
              
              
                
                  
                
                
                
                
              
              
                
                
              
              
                
                
              
              
                
                
              
              
                
                
              
              
                
                
              
              
                
                
              
            
          
          
          
            
              
              
              
                
                  
                    
                      
                      
                      
                    
                  
                  
                    
                      
                      
                    
                  
                  
                    
                      
                      
                    
                  
                
                
                  
                    
                      
                      
                      
                    
                  
                  
                    
                      
                      
                    
                  
                  
                    
                      
                      
                    
                  
                
                
                  
                    
                  
                  
                    
                  
                
                
                  
                    
                  
                  
                    
                  
                
                
                  
                    
                  
                  
                    
                  
                
                
                  
                    
                  
                  
                    
                  
                
              
            
          
        
        
        
          
            
              
            
          
          
            
              
            
          
          
          
            
              
              
              
              
                
                
                
              
              
              
              
                
                
              
              
                
                
              
              
              
                
                
                
              
              
                
                
              
              
                
                
              
              
                
                
                
              
              
                
                
              
            
          
        
      
    

提交回复
热议问题