none
Creating instace in EF5 objectContext is setting the non null properties to zero.. RRS feed

  • Question

  •                 

    Hello guys!
     I'm getting a problem when creating an instance of an entity in entity framework 5.
     I'm creating it by reflection:

    var method = this.objectContextinstance().GetType().GetMethod("CreateObject");
    method = method.MakeGenericMethod(Type.GetType("PERSON"));
    var entity = method.Invoke(this.objectContextinstance(), null);



      the problem is that the not null properties are beeing initialized with zero, and i don't want it to.
    Because if i save it it makes a referece to another object that key is zero.

    is there a way of creating an instance and it not automaticly set the non null properties?

    Thanks!

    Thursday, March 28, 2013 1:18 PM

Answers

  • The non reflection stuff was not to solve the issue but an attempt to better understand if it was a problem in your class (not knowing which datatype you used, I assumed a nullable type) in this particular context.

    I'm not sure which behavior you expect. My understanding is now that you are just using an int and its default value is 0 as usual. Which default value would you expect for this ? If null, you would have to use the "int?" datatype (http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx ) but saving a person with a null address is perhaps not allowed as well ?

    It seems to me you are just missing to add some validation rule so that you can display a message if the person object is not currently valid when the user tries to save it. Not sure which UI/binding validation technology you are using (for example you could use "DataAnnotations").

     


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

    Saturday, March 30, 2013 10:49 AM

All replies

  • Hi,

    Do you have the same problem when not using reflection ? Also knowning the property type could help. In particular if they are int properties this is just the expected behavior.

    You said "not null properties are beeing initialized with zero, and i don't want it to" but if they really are NOT NULL properties they can't be initialized to null so they have just their default value...

    So double check the type you used. If you used int you should use int? instead. Note that if this is a primary key server side generated value this is not an issue (0 will be replaced by the server side generated value when the entity will be saved).

    If it doesn't help, as you can see some more context could help to better understand your issue.


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

    Friday, March 29, 2013 5:13 PM
  • Hello and thanks for the repply,

       in the context of my project the reflection is the only way.

       I think i explained myself in the wrong way.

       Lets say i have a form in WPF that creates a Person and in that is an option to choose the address.

       When creating an instance of Person the CODE_Address (int32) is automatically set to zero, and if i don't chose an address in the form, saving the Person  makes his address reference to the address that has the code zero.

       In the form, im detecting the errors on try saving, so i can alert the users what fields are wrong.

       Has you have figured when the code is set to zero and saving the model automatically sets the navigation property to that reference in the address table.

      As i said before, i think it wold be a good thing  that it not initializes this properties, so when saving, it alert that those fields are mandatory.

       

    Many thanks in advance!

      

    Friday, March 29, 2013 6:12 PM
  • The non reflection stuff was not to solve the issue but an attempt to better understand if it was a problem in your class (not knowing which datatype you used, I assumed a nullable type) in this particular context.

    I'm not sure which behavior you expect. My understanding is now that you are just using an int and its default value is 0 as usual. Which default value would you expect for this ? If null, you would have to use the "int?" datatype (http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx ) but saving a person with a null address is perhaps not allowed as well ?

    It seems to me you are just missing to add some validation rule so that you can display a message if the person object is not currently valid when the user tries to save it. Not sure which UI/binding validation technology you are using (for example you could use "DataAnnotations").

     


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

    Saturday, March 30, 2013 10:49 AM