EF6 Table-per-Hierarchy (TPH) - abstract base class with abstract property does not generate a migration












1















I have an abstract base class set up for TPH:



public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}


.. and I inherit from it in other derived classes as per:



public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}


This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.



It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!










share|improve this question


















  • 1





    The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.

    – Ivan Stoev
    Nov 22 '18 at 17:13






  • 1





    Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?

    – Ciaran
    Nov 23 '18 at 9:36











  • Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.

    – Ivan Stoev
    Nov 23 '18 at 10:56
















1















I have an abstract base class set up for TPH:



public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}


.. and I inherit from it in other derived classes as per:



public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}


This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.



It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!










share|improve this question


















  • 1





    The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.

    – Ivan Stoev
    Nov 22 '18 at 17:13






  • 1





    Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?

    – Ciaran
    Nov 23 '18 at 9:36











  • Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.

    – Ivan Stoev
    Nov 23 '18 at 10:56














1












1








1








I have an abstract base class set up for TPH:



public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}


.. and I inherit from it in other derived classes as per:



public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}


This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.



It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!










share|improve this question














I have an abstract base class set up for TPH:



public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}


.. and I inherit from it in other derived classes as per:



public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}


This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.



It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!







c# entity-framework-6 tph






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 17:06









CiaranCiaran

12918




12918








  • 1





    The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.

    – Ivan Stoev
    Nov 22 '18 at 17:13






  • 1





    Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?

    – Ciaran
    Nov 23 '18 at 9:36











  • Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.

    – Ivan Stoev
    Nov 23 '18 at 10:56














  • 1





    The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.

    – Ivan Stoev
    Nov 22 '18 at 17:13






  • 1





    Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?

    – Ciaran
    Nov 23 '18 at 9:36











  • Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.

    – Ivan Stoev
    Nov 23 '18 at 10:56








1




1





The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.

– Ivan Stoev
Nov 22 '18 at 17:13





The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.

– Ivan Stoev
Nov 22 '18 at 17:13




1




1





Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?

– Ciaran
Nov 23 '18 at 9:36





Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?

– Ciaran
Nov 23 '18 at 9:36













Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.

– Ivan Stoev
Nov 23 '18 at 10:56





Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.

– Ivan Stoev
Nov 23 '18 at 10:56












1 Answer
1






active

oldest

votes


















1














Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:



public abstract class BaseSchema
{
...
public virtual bool IsFilterRequired { get; set; }
...
}


As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.



public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
{
...
public override bool IsFilterRequired
{
get { return false; }
set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
}
...
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435590%2fef6-table-per-hierarchy-tph-abstract-base-class-with-abstract-property-does%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:



    public abstract class BaseSchema
    {
    ...
    public virtual bool IsFilterRequired { get; set; }
    ...
    }


    As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.



    public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
    {
    ...
    public override bool IsFilterRequired
    {
    get { return false; }
    set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
    }
    ...
    }





    share|improve this answer




























      1














      Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:



      public abstract class BaseSchema
      {
      ...
      public virtual bool IsFilterRequired { get; set; }
      ...
      }


      As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.



      public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
      {
      ...
      public override bool IsFilterRequired
      {
      get { return false; }
      set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
      }
      ...
      }





      share|improve this answer


























        1












        1








        1







        Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:



        public abstract class BaseSchema
        {
        ...
        public virtual bool IsFilterRequired { get; set; }
        ...
        }


        As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.



        public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
        {
        ...
        public override bool IsFilterRequired
        {
        get { return false; }
        set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
        }
        ...
        }





        share|improve this answer













        Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:



        public abstract class BaseSchema
        {
        ...
        public virtual bool IsFilterRequired { get; set; }
        ...
        }


        As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.



        public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
        {
        ...
        public override bool IsFilterRequired
        {
        get { return false; }
        set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
        }
        ...
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:16









        CiaranCiaran

        12918




        12918






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435590%2fef6-table-per-hierarchy-tph-abstract-base-class-with-abstract-property-does%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin