none
Code First - Setting null in a many-to-one property RRS feed

  • Question

  • Hi, I am using Code-First from these two entities:

     

    public partial class Entidad2
    {
    		// Keys
    		public Int32 Id { get; set; }
    
    		// Scalar properties
    		public String Property { get; set; }
    
    		// Navigation properties (many-to-one)
    		[InverseProperty("Entidad2")]
    		public virtual Entidad1 Entidad1 { get; set; }
    }
    
    public partial class Entidad1
    {
    		// Keys
    		public Int32 Id { get; set; }
    
    		// Scalar properties
    		public String Property { get; set; }
    
    		// Navigation properties (one-to-many)
    		private ICollection<Entidad2> entidad2;
    		[InverseProperty("Entidad1")]
    		public virtual ICollection<Entidad2> Entidad2
    		{ 
    			get { return entidad2 ?? (entidad2 = new HashSet<Entidad2>()); }
    			set { entidad2 = value; } 
    		}
    }

    Then, when I tried to set Entidad2.Entidad1 property to null, property gets unaffected:

     

    Entidad2 entidad2 = _entidad2Service.Find(entidad2Model.Id);
    //Here entidad2.Entidad1 is not null
    entidad2.Entidad1 = null; // It is not working
    Assert.IsTrue(entidad2.Entidad1 == null)  //Error
    

     


    Am I missing anything?? I suppose proxy is causing this effect, but how could I set to null that property??

    Thanks in advance! Regards


    Ruben J. Marrufo
    Thursday, October 20, 2011 1:59 PM

Answers

  • Hi! I saw this article http://blogs.realdolmen.com/experts/2011/02/17/understanding-entity-framework-associations/ and I've realized that there is an issue with Entity Framework "that a reference property is not considered changed if it is set to null without first reading its value. As a result the following code does not work as expected" // Perform update:

    using (var context = new OrderEntities())
    {
        Order order = context.Orders.First(x => x.Id == 1);
        // dummy = order.Customer; // solves the issue
        order.Customer = null;
        context.SaveChanges(); 
    } 



    Ruben J. Marrufo
    Monday, October 24, 2011 4:02 PM

All replies

  • Hi RubenJMarrufo,

    I did a test of your scenario and ended up not being able to reproduce the problem if I directly use the DbContext. 

     MyContext db = new MyContext();
    
     Entidad2 entidad2 = db.Entitdad2s.Where(x => x.Id==1).Single();
                                         
    entidad2.Entidad1 = null; 
    

    It always resulted in null when I set it, and it definitely had a value before i set it to null.

    So yes, it must be something to do with how the entity is being returned from the service and using the Proxy.  What is the definition of the proxy class for Entidad2?  I would like to see how it defines the Entidad1 property in such a way it can't be set as null.


    Tom Overton
    • Edited by Tom_Overton Thursday, October 20, 2011 4:32 PM
    Thursday, October 20, 2011 4:30 PM
  • Thanks Tom! The service call is just like yours:        
    public virtual Entidad2 Find(int id)
            {
                return EntitiesDB.Entidad2Set.Find(id);
            }
    Is it possible to see the definition of the proxy?? How could I do that?? I think there is the key, too.
    Regards!

    Ruben J. Marrufo
    Friday, October 21, 2011 2:14 PM
  • Just right click on Entidad2 and select "Go to Definition".
    Tom Overton
    Friday, October 21, 2011 2:32 PM
  • If I do that I will see Entidad2 definition that I copied in my first post. I mean to see the real code of the dynamically created proxy of Entidad2: ((System.Data.Entity.DynamicProxies.Entidad2_4A5FE5A457B2B0CC617F7725A2EB4B1B8B5F823F763CCF076F55C9C0F6B78253)(entidad2))

    Ruben J. Marrufo
    Monday, October 24, 2011 9:40 AM
  • Hi! I uploaded an example solution to my SkyDrive. The test 'ChangeEntidad2Test' in 'Service.Tests' project shows my issue, and it's not passing.

    https://skydrive.live.com/?cid=7be8eec705fcce19&sc=documents&id=7BE8EEC705FCCE19%21200

    Thanks in advance!!


    Ruben J. Marrufo
    Monday, October 24, 2011 12:26 PM
  • Hi! I saw this article http://blogs.realdolmen.com/experts/2011/02/17/understanding-entity-framework-associations/ and I've realized that there is an issue with Entity Framework "that a reference property is not considered changed if it is set to null without first reading its value. As a result the following code does not work as expected" // Perform update:

    using (var context = new OrderEntities())
    {
        Order order = context.Orders.First(x => x.Id == 1);
        // dummy = order.Customer; // solves the issue
        order.Customer = null;
        context.SaveChanges(); 
    } 



    Ruben J. Marrufo
    Monday, October 24, 2011 4:02 PM