none
When set the property with one-to-one relationship of an entity, the entity is null. RRS feed

  • Question

  • The problem is encountered when I am using the ASP.NET role and membership model schema. It happens to the membership.
    [Association(Name="aspnet_Users_aspnet_Membership", Storage="_aspnet_Users", ThisKey="UserId", OtherKey="UserId", IsForeignKey=true)]
    		public aspnet_Users aspnet_Users
    		{
    			get
    			{
    				return this._aspnet_Users.Entity;
    			}
    			set
    			{
    				aspnet_Users previousValue = this._aspnet_Users.Entity;
    				if (((previousValue != value) 
    							|| (this._aspnet_Users.HasLoadedOrAssignedValue == false)))
    				{
    					this.SendPropertyChanging();
    					if ((previousValue != null))
    					{
    						this._aspnet_Users.Entity = null;
    						previousValue.aspnet_Membership = null;
    					}
    					this._aspnet_Users.Entity = value;
    					if ((value != null))
    					{
    						value.aspnet_Membership = this;
    						this._UserId = value.UserId;
    					}
    					else
    					{
    						this._UserId = default(System.Guid);
    					}
    					this.SendPropertyChanged("aspnet_Users");
    				}
    			}
    		}
    The first line this._aspnet_Users.Entity is null.
    But the same thing doesn't happen to this._aspnet_Applications.Entity.
    Why is that?
    Tuesday, November 10, 2009 4:20 AM

Answers

  • I am using L2Q, it is a ASP.NET project.
    I switch to memberwiseClone() and it seems to work.
    Here is the code that generates the problem.

      public class DetachableEntity<T> where T : DetachableEntity<T>, new()
        {
            public T original { get; set; }
    
            public void OnDataLoaded()
            {
                original = Clone();
            }        
         
            public T Clone()
            {
                return Clone((T)this);
            }
    
            public static T Clone(T item)
            {
                if (item == null)
                    return null;
    
                T newItem = new T();
    
                // copy all subclass properties.
                foreach (PropertyInfo prop in item.GetType().GetProperties())
                {
                    PropertyInfo prop2 = item.GetType().GetProperty(prop.Name);
                    
                   if (prop2.CanWrite)
                    {
                        prop2.SetValue(newItem, prop.GetValue(item, null), null);
                    }
                    else
                    {
                        throw new Exception(string.Format("{0} can't write", prop2.Name));
                    }
    
                    
                }
    
                
                //the two items now share the same orginal state object, fix this by
                //cloning the original state object on the item that to create a new original state
                //object for the new item
    
                if (item.original != null)
                    newItem.original = item.original.Clone();
    
                return newItem;
            }
        }
            internal override aspnet_Membership GetMembership(Guid memberId)
            {
                aspnet_Membership l_result;
                using (LinqDataClassesDataContext l_linqContext = new LinqDataClassesDataContext(this.m_connStr))
                {
                    l_result = l_linqContext.aspnet_Membership.SingleOrDefault(member => member.UserId == memberId);                
                }
                return l_result;
            }
    • Marked as answer by Maxi Ng Friday, November 13, 2009 7:42 AM
    Thursday, November 12, 2009 7:36 AM

All replies

  • Hi Chui,

     

    Are you using LinqToSql or Entity Framwork when you metion 1:1relationship?

    Is it an ASP.NET project you are working with?

     

    Could you provide the related entities information and the code that you are working with (not the generated code)?

     

     

    Please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, November 12, 2009 6:17 AM
  • I am using L2Q, it is a ASP.NET project.
    I switch to memberwiseClone() and it seems to work.
    Here is the code that generates the problem.

      public class DetachableEntity<T> where T : DetachableEntity<T>, new()
        {
            public T original { get; set; }
    
            public void OnDataLoaded()
            {
                original = Clone();
            }        
         
            public T Clone()
            {
                return Clone((T)this);
            }
    
            public static T Clone(T item)
            {
                if (item == null)
                    return null;
    
                T newItem = new T();
    
                // copy all subclass properties.
                foreach (PropertyInfo prop in item.GetType().GetProperties())
                {
                    PropertyInfo prop2 = item.GetType().GetProperty(prop.Name);
                    
                   if (prop2.CanWrite)
                    {
                        prop2.SetValue(newItem, prop.GetValue(item, null), null);
                    }
                    else
                    {
                        throw new Exception(string.Format("{0} can't write", prop2.Name));
                    }
    
                    
                }
    
                
                //the two items now share the same orginal state object, fix this by
                //cloning the original state object on the item that to create a new original state
                //object for the new item
    
                if (item.original != null)
                    newItem.original = item.original.Clone();
    
                return newItem;
            }
        }
            internal override aspnet_Membership GetMembership(Guid memberId)
            {
                aspnet_Membership l_result;
                using (LinqDataClassesDataContext l_linqContext = new LinqDataClassesDataContext(this.m_connStr))
                {
                    l_result = l_linqContext.aspnet_Membership.SingleOrDefault(member => member.UserId == memberId);                
                }
                return l_result;
            }
    • Marked as answer by Maxi Ng Friday, November 13, 2009 7:42 AM
    Thursday, November 12, 2009 7:36 AM
  • Hi Maxi,

     

    Do you mean you solve the problem now?

     

    Since you are working with ASP.NET project, I recommend you to post in ASP.NET forums,

    http://forums.asp.net/

     

    There are more experts on ASP.NET in those forums.

     

    If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, November 13, 2009 7:27 AM
  • Yes, I get it. thank you.
    Friday, November 13, 2009 7:42 AM