locked
EF 6.1: Abstract Base Class RRS feed

  • Question

  • Upgrading Entity Framework from 6.0.2 to 6.1 caused an Exception building my DBContext. 

    The abstract type 'Test1Data' has no mapped descendants and so cannot be mapped. Either remove ' Test1Data ' from the model or add one or more types deriving from ' Test1Data ' to the model. 

    The situation is I have to classes (Test1 and Test2)  in different projects but no reference between these projects and I want to have a relationship in my database :

    Public Class Test1
      Public Property Test2 As Test2 'This is not working

    End Class

    Public Class Test2
    .....
    End Class

    To get the relationship in my database before EF 6.1 I made a class in my data layer (where I have the references to the different projects):

    Public MustInherit Class Test1Data
    Inherits Test1

    Public Property Test2 As Test2
    End Class

    Until now this worked for me. EF made the right relation in my database. I declared the class 'MustInherit' because I did not want to have a Discriminator column in my database. 

    But this is not working anymore. Can anybody please help me ?

    Tuesday, March 25, 2014 10:45 AM

Answers

  • Hello Wolfgang,

    I am trying to help look into this issue.

    With your model, I tested it both with EF6.0.2 and EF 6.1.0, however, both will give me the error which you met. Here is the context class:

    Partial Public Class S26DbContext
    
        Inherits DbContext
    
        Public Property Test1Datas As DbSet(Of Test1Data)
    
        Public Property Test2s As DbSet(Of Test2.Test2)
    
        Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    
            MyBase.OnModelCreating(modelBuilder)
    
        End Sub
    
    End Class
    

    And as far as I know that MustInherit is to VB.NET as abstract is to C# and an abstract class cannot be mapped to database directly so I remove the MustInherit and then it works fine, it will not create the discriminator column in the database. You can check the table schema generated in my database:

    CREATE TABLE [dbo].[Test2] (
    
        [Id]   INT            IDENTITY (1, 1) NOT NULL,
    
        [Name] NVARCHAR (MAX) NULL,
    
        CONSTRAINT [PK_dbo.Test2] PRIMARY KEY CLUSTERED ([Id] ASC)
    
    );
    
    CREATE TABLE [dbo].[Test1Data] (
    
        [Id]       INT            IDENTITY (1, 1) NOT NULL,
    
        [Name]     NVARCHAR (MAX) NULL,
    
        [Test2_Id] INT            NULL,
    
        CONSTRAINT [PK_dbo.Test1Data] PRIMARY KEY CLUSTERED ([Id] ASC),
    
        CONSTRAINT [FK_dbo.Test1Data_dbo.Test2_Test2_Id] FOREIGN KEY ([Test2_Id]) REFERENCES [dbo].[Test2] ([Id])
    
    );
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_Test2_Id]
    
        ON [dbo].[Test1Data]([Test2_Id] ASC);
    

    You can have a try to delete the MustInherit.

    If this does not work for you, please upload your project to skydriver so that we can compare yours with ours to check whether there is different.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Wednesday, April 2, 2014 7:38 AM
    Wednesday, March 26, 2014 3:02 AM