none
Interface Object Based on an Entity works for direct Properties but not a New Property RRS feed

  • Question

  • I have a Linq entity where I have added an interface to the class:

    public class IProposalAttribute
    {
    
        public string Name;
        public string Description;
        public string ID;               // Not on the target Entity
        public bool Archived;
    
    }
    Now the target entity has the columns Name, Description and Archived,  but it doesn't have ID. So on the partial class I define ID extracted from the primary key of the entity column:

    public partial class Customer : IProposalAttribute
    {
        public string ID
        {
            get { return this.Customer_ID.ToString(); }
            set
            {
                try
                {
                    this.Customer_ID = int.Parse( value );
                }
                catch ( Exception ) { }
            }
    
        }
    
    }
    The problem is when I try to get all the ID data when the entity is returned.

    IQueryable<IProposalAttribute> TableData = null;
    DataLoadOptions options = new DataLoadOptions();
    
    options.LoadWith<Customer>( c => c.Customer_ID ); // Customer returns with Customer_ID (this may be superflous)
    options.LoadWith<IProposalAttribute>( ip => ip.ID ); // Apparently has no effect.
    DataContext.LoadOptions = options;
    
    TableData = DataContext.Customers.Cast<IProposalAttribute>();
    
    
    var items = from it in TableData
                select new
                {
                    ID = it.ID,               // This is always null
                    Name = it.Name,                 // Ok
                    Description = it.Description,  // OK
                    GroupName = it.Name,        // OK
                    Archived = it.Archived         // OK
    
                };
    

    The query returns all the columns including Customer_ID, the cast works, but when I view the interface object in the debugger (or when ID is used in code), ID is always null.

    What am I missing?

    A
    William Wegerson (www.OmegaCoder.Com)
    Tuesday, February 9, 2010 8:10 PM
    Moderator

Answers

  • Hi William,

     

    It’s really great to see you here!   J

     

    If no [Column] attribute is added, by default LINQ to SQL maps the column and the entity property via their names.  For example, the ID property maps the ID column and the Name property maps to the Name column.   If the property name is not the same as the column name, we need to use the [Column] attribute to set the column information for the property. 

     

    In this case, LINQ to SQL will retrieve all the column values from the Customer table.  However, when no column maps to the ID property, so LINQ to SQL cannot set its value.   As a workaround, we can set the [Column] attribute on the ID property of the Customer partial class.

    =================================================================

        public partial class Customer : IProposalAttribute

        {

            [Column(Name = "Customer_ID")]

            public string ID

           

        }

    =================================================================

     

    Does this work fine at your side?    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    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.
    Wednesday, February 10, 2010 6:42 AM
    Moderator

All replies

  • Hi William,

     

    It’s really great to see you here!   J

     

    If no [Column] attribute is added, by default LINQ to SQL maps the column and the entity property via their names.  For example, the ID property maps the ID column and the Name property maps to the Name column.   If the property name is not the same as the column name, we need to use the [Column] attribute to set the column information for the property. 

     

    In this case, LINQ to SQL will retrieve all the column values from the Customer table.  However, when no column maps to the ID property, so LINQ to SQL cannot set its value.   As a workaround, we can set the [Column] attribute on the ID property of the Customer partial class.

    =================================================================

        public partial class Customer : IProposalAttribute

        {

            [Column(Name = "Customer_ID")]

            public string ID

           

        }

    =================================================================

     

    Does this work fine at your side?    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    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.
    Wednesday, February 10, 2010 6:42 AM
    Moderator
  • No Lingzhi Sun...its great to see you here. :-)

    Thanks, that is what I was missing; the propagation of the mapping into the partial. Again, I appreciate your help!

    Xie Xie!
    William Wegerson (www.OmegaCoder.Com )
    Wednesday, February 10, 2010 4:11 PM
    Moderator
  • Bu Yong Xie (You're welcome), William!  J


     
    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    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, February 11, 2010 8:37 AM
    Moderator