none
Why when using TPH inheritance are default values ignored? RRS feed

  • Question

  • hi

        CREATE TABLE Lodging
        (
             LodgingID int PRIMARY KEY IDENTITY,
             LodgingName nvarchar(100),
             Resort bit NOT NULL,
             ResortChainOwner nvarchar(100) NOT NULL,
                 ...
        )
    


    Lodging DB table maps to Lodging entity. I've then implemented a TPH inheritance: 

    • With Lodging entity being the base
    • I've created a new entity Resort, which derives from Lodging 
    • I moved resort-specific property ResortChainOwner to the Resort entity type and also set ( in SSDL ) ResortChainOwner's Default Value to "notApplicable"
    Lodging table's Resort column  is a discriminatory column. If Resort column contains `0`, then records is materialized into Lodging entity and if column contains `1`, then record is materialized into Resort entity


    When in the following code SaveChanges is called, EF should supply (to Insert command) a default value for ResortChainOwner field, but instead it supplies a Null:
                var lodging = new Lodging();
                ...
                context.Lodgings.AddObject(lodging);
                context.SaveChanges();
    

    So how do I on inserts force EF to supply a default value for ResortChainOwner column?

    Thank you








    • Edited by KlemS100 Thursday, January 12, 2012 10:59 PM
    Wednesday, January 11, 2012 8:09 PM

All replies

  • Hi KlemS100,

    Welcome to MSDN Forum.

    If you assing a default value to a Resort entity, but you create a Lodging entity with the property 'Resort' as 0, the default entity will not works.

    By the way, in the database, the 'ResultChainOwen' is not null, but if you don't create a 'Resort' entity, just create a 'Lodging' property, the project will throw an exception. Based on you moved the property to Resort entity, I assume when you compile the project, there will be an error thrown in your .edmx file. Because the ResortChainOwner has no default value and it is not nullable.

    Here's a link which may help you with TPH

    http://msdn.microsoft.com/en-us/library/cc716683.aspx

    Best Regards 


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, January 13, 2012 5:46 AM
    Moderator
  • You've misread my post. I didn't assign a default value to Resort property, I've assigned default value to ResortChainOwner property

    "Based on you moved the property to Resort entity, I assume when you compile the project, there will be an error thrown in your .edmx file. Because the ResortChainOwner has no default value and it is not nullable."

    Project doesn't throw an error on compile since ResortChainOwner has a default value set to "notApplicable" ( I've set it in SSDL section of EDMX file )

    Friday, January 13, 2012 4:32 PM
  • Hi KlemS100,

    Forgive my dull, I still can't repro the scenario. How did you make the filed Resort as discriminatory? When I add a condition in the mapping details window, set the Resort to 0 in Lodging entity and set it to 1 in Resort entity, the .edmx file will throw an exception.

    Here's the model I created

    By the way, I didn't create 'Resort' table in my database, because the T-SQL you post here is only the 'Lodging' table creation script, I assume that you have a 'Lodging' table in your database an use Database First.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 7:34 AM
    Moderator
  • "When I add a condition in the mapping details window, set the Resort to 0 in Lodging entity and set it to 1 in Resort entity, the .edmx file will throw an exception."

    Yes, that's how I did it. 


    "...the .edmx file will throw an exception."

    Emdx throws an exception because ResortChainOwner is non-nullable and doesn't have a default value.  When we set ( in SSDL ) ResortChainOwner's Default Value to "notApplicable", then .edmx stops throwing an exception


    "By the way, I didn't create 'Resort' table in my database, because the T-SQL you post here is only the 'Lodging' table creation script, I assume that you have a 'Lodging' table in your database an use Database First."

    Yes, I used a Database First and yes, Resort table doesn't exist in a database




    • Edited by KlemS100 Tuesday, January 17, 2012 8:49 PM
    Tuesday, January 17, 2012 8:45 PM
  • Hi KlemS100,

    The exception is not about default value, it is "Condition member Lodging.Resort' with a condition other than 'IsNull=False' is mapped. Either remove the condition on Lodging.Resort or remove it from the mapping".

    If possible, please send the project to a free space and post the link here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 19, 2012 3:31 AM
    Moderator