locked
EF Code First and abstract base class RRS feed

  • Question

  • I would like to ask confirmation whether or not adding an abstract base class to an entity poses any potential problem.
     
    We had a simple model:
    public class Tabel1
    {
     public int Id { get; set; }
     public string Field1 { get; set; }
    }
    
    public class Tabel2
    {
     public int Id { get; set; }
     public string Field1 { get; set; }
    }
    
    No mappings whatsoever, just plain tables. Then we changed to:
    public abstract class BaseTable
    {
     public abstract int Id { get; set; }
    }
    
    public class Tabel1 : BaseTable
    {
     public override int Id { get; set; }
     public string Field1 { get; set; }
    }
    
    public class Tabel2 : BaseTable
    {
     public override int Id { get; set; }
     public string Field1 { get; set; }
    }

    because we want to use the baseclass as a generic constraint elsewhere in our code.

    Can this cause any known issues or sideeffects?

    Regards.


    Jan C. de Graaf
    Friday, May 6, 2011 10:44 AM

Answers

  • Hello Jan C. de Graaf,

     

    Nice to see you.

    According to your description, I think this familiar issue has been answered by Diego who is from EF design Team. I quote his reply here:

    "

    What you are describing seems to match a an issue we found very late in EF 4.1 RTW cycle and we decided to postpone because our evaluation indicated that it had very low impact: while this unnecessary table is created for an abstract base type, the table does not participate in any mapping, therefore it shouldn't appear in any query produced by EF.

    For case in which you are using Code First to generate the database, the table will be added to the database schema but never used. For cases in which you are using the Code First API to map to an existing database it shouldn't have an impact either, since no queries generated by EF will every fail because the table is not there.

    If you are seeing other behavior or higher impact, please let us know.

    By the way, I am not aware of a workaround tha just removes the table without changing the shape of your model.

    Hope this helps,
    Diego

    "
    More information, please read this thread: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/30086074-6773-4b83-bed5-88a401fb2a4e

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Tuesday, May 17, 2011 4:55 AM
    Monday, May 9, 2011 7:48 AM