none
Inheritance and code first RRS feed

  • Question

  • hello,

    The question is: how to prevent EntityFramework to alter the table structure when I inherit an entity ?
    I use Code First.

    I have the entity ce1 as

    public class ce1 {
        public Int32 Id {get; set;}
        public String v {get; set;}
    }

    If I add the following class (to map entities to a DataGridView in a WinForm app):

    public class ce2 : ce1 {
        public Boolean Selected {get; set;}
    }

    Then EF expect to find a "discriminator" column in the table storing the ce1 entities.
    How to avoid this behaviour ?

    Actually I use the following :

    public class ce1 {
        public Int32 Id {get; set;}
        public String v {get; set;}

        public Boolean Selected {get; set;}
    }

    And I build the model with the following :
    Ignore(e => e.Selected)

    But I'm not satisfied. I can expose other cases in which I actually use 

    class dao1 {
        public ce1 {get; set;}
        public AggregationClass {get; set;}
    }

    thank you in advance


    thierry

    Saturday, March 24, 2012 5:26 PM

Answers

  • If you use NotMapped the schema is still altered ? It seems to work here (EF 4.1).

    My personal preference would be to not have ce2 entities at all but but rather to just store ce1 entities in a list. In addition it allows to handle distinct lists depending on the purpose which is not possible with a single boolean property.

    If you meant you don't want this to be the default, this is likely not possible. If you store an entity and inherit from this entity, by default EF will prepare the schema so that both can be stored as both are "reachable" from the context.

    If you meant you want to allow third party to inherit from your entities regardless of this issue, you could perhaps have entity create base:entity (not mapped) then I assume that when one inherits from base it won't be mapped (don't had time to check).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".



    Monday, March 26, 2012 9:03 AM

All replies

  • Hi,

    This is expected. How is declared the context ? It seems you want to store either ce1 and ce2 entities so EF creates the discrimator columns so that it can store both entity types. Is this not what you want ?

    How is declared the context ? In dao1 you do use ce1 so you want to be able to store both ce1 and ce2 entities ?

    Try to explain what is the behavior you want as for now it seems just the expected behavior to support doing what you asked for...

    Or if Selected is something unrelated to the entity you could use a separate collections to store user selected instances (not sure what is the purpose of Selected) or use only ce1 with a NotMapped attribute ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Saturday, March 24, 2012 7:15 PM
  • hello, and thank you for the reply.

    I don't want to store the property. I actually use a NotMapped in the entity.

    But I would like to be able to inherit from the POCO entity without altering the database schema.

    I hope I'm clear enough.

    thank you again

    thierry


    thierry

    Sunday, March 25, 2012 6:31 PM
  • If you use NotMapped the schema is still altered ? It seems to work here (EF 4.1).

    My personal preference would be to not have ce2 entities at all but but rather to just store ce1 entities in a list. In addition it allows to handle distinct lists depending on the purpose which is not possible with a single boolean property.

    If you meant you don't want this to be the default, this is likely not possible. If you store an entity and inherit from this entity, by default EF will prepare the schema so that both can be stored as both are "reachable" from the context.

    If you meant you want to allow third party to inherit from your entities regardless of this issue, you could perhaps have entity create base:entity (not mapped) then I assume that when one inherits from base it won't be mapped (don't had time to check).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".



    Monday, March 26, 2012 9:03 AM
  • hello,

    All that's you say sounds right.

    My first goal was to populate a List<entity + booleanForCheckBox> with enties in order to bind those data to a DataSource of a DataGridView for example.

    Then enumerate all the checked line of the DataSource and add them to the dbset by casting the DataSource type do entity type... as DataSource inherit from entity that's sounded good... but that was before I discover the discriminator and the management of inheritance by EF.

    Now I use NotMapped for my boolean in the entity and that's do the job.

    My problem is now a bit far away.... I would like to be able to have an inherited type with aggregation data and... an optimal Transact-sql query : new problem

    thank you again


    thierry

    Monday, March 26, 2012 1:35 PM