none
Unable to update a boolean property to false on a derived class... RRS feed

  • Question

  • Hi,

    I am unable to use EF to update the boolean property of a derived class. More specifically, I cannot set the value to false but I can set it to true.

    For example, let's say I have two tables in my database: Person and Customer. Both use PersonId as a primary key. All customers are people but not all people are customers. The person table has a key column and a string column (name). The customer table has a key column and a bit column (IsActive).

    I create a silverlight app to test the problem. I add an Entity Data Model (edmx). I have a person entity and a customer entity. I set it so customer inherits from person.

    I add a domain service, make person (and therefore customer also) editable and generate the metadata.

    Let's say I have one record in my database. In the Person table,  PersonId = 1 and Name = Bob. In the customer table, PersonId = 1 and IsActive = 1 (true).

    If I edit this record in my application and set  IsActive to false and save changes to the database, it does not update. However, if IsActive is false in the database and I set it to true in the application and save, this works.

    I checked the ChangeSet of the DomainService in the generated Update method and the boolean property is there. When I run SQL Profiler, the SQL generated only includes the boolean property if it is being set to true, otherwise it is not included.

    If I have a boolean on the base class (Person), I can toggle between them with no problem. Also, if I add other properties to the derived class they work fine as well.

    I have no default values set for the boolean property in the entity model nor in the database. My application has several boolean properties in derived classes and if I cannot set them, I cannot use EF for saves.

    Any ideas?

    Thanks in advance.

     

     


    • Edited by splitpete Thursday, September 22, 2011 3:06 PM
    Thursday, September 22, 2011 3:04 PM

Answers

All replies

  • Hi splitpete,

    Welcome!

    Would you please give us more detail information about the update code, I want to know the logic in EF instead of domain service as I'm not familiar with Silverlight, thanks for understanding.

    Have a nice day.


    Alan Chen[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.

    Friday, September 23, 2011 9:07 AM
    Moderator
  • Hi,

    Thank you for your reply.

    My Update code is as follows. It is generated code when I created my Domain Service and opted to make the entities editable:

    		public void UpdatePerson(Person currentPerson) {
    
    			this.ObjectContext.People.AttachAsModified(currentPerson, this.ChangeSet.GetOriginal(currentPerson));
    		}
    

    As I mentioned above, changes show up in the ChangeSet object when I modify the IsActive value to true or false in the application and save. The save calls the method above.

    I set up a SQL Profile trace to see what SQL code was generated. If the IsActive value is true, it is included in the generated SQL. If the IsActive value is false, it is not.

    This makes me think it is a problem with EF and not RIA. Perhaps you have some other ideas. I am interested to hear what you think and if you can reproduce the problem.

    Thanks again.

     

    Friday, September 23, 2011 9:29 AM
  • Hi splitpeter,

    Thanks for your feedback.

    I try to reproduce your scenario, but it seems work on my computer, I'll show my steps:

    1. create two tables: Person(Id pk, Name nvarchar(20)), Customer(Id PK, Name nvarchar(20), IsActive bit);

    2. Add EF edmx to my Console project-->add the two tables

    3. Create a inheritance from Person to Customer and delete "Name" and "Id" from Customer

    4.

     using (var context= new EFTestEntities())
                { 
                    var customer = new Customer() {  Id=1, IsActive=false, Name="Bob"};
                    context.People.Attach(customer);
                    context.ObjectStateManager.ChangeObjectState(customer, System.Data.EntityState.Modified);
                    context.SaveChanges();
                }
    

    it works. I think you can test it without Domain service to see if it works.

    Have a nice day.


    Alan Chen[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.

    Monday, September 26, 2011 8:38 AM
    Moderator
  • Thank you for your reply. You are correct. I am able to make this work without using RIA Services. My application uses RIA Services so I still need to solve this issue or find a work around. Can you try and reproduce it? Should I repost this question on the RIA Services forum?

     

    Monday, September 26, 2011 11:01 AM
  • Hi splitpete,

    I'm not good at it, thanks for understanding. In order to get better support, I think you should repost your question here: http://forums.silverlight.net/53.aspx, thanks for understanding.

    Have a nice day.


    Alan Chen[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 splitpete Tuesday, September 27, 2011 9:35 AM
    Tuesday, September 27, 2011 6:04 AM
    Moderator
  • Ok, I will repost it in the other forum. Thank you for your help so far.
    Tuesday, September 27, 2011 9:34 AM