none
Help with GetModifiedMembers InvalidOperationException (entity is not of the correct type) RRS feed

  • Question

  • Any idea about what is causing this error would be greatly appreciated.
    How can an instance of MailingRecord selected from IQueryable<MailingRecord> by FirstOrDefault() not be a MailingRecord for GetModifiedMembers() witin a context instance?

    System.InvalidOperationException was unhandled
      Message="The entity is not of the correct type."
      Source="System.Data.Linq"
      StackTrace:
           at System.Data.Linq.Table`1.GetModifiedMembers(TEntity entity)
    ...

    using (MailingEntityContext context = new MailingEntityContext(_WriteSources.ConnectionString, _WriteMapSource))
    
    {
    
    	IQueryable<MailingRecord> testUpdate =
    
    		from mailRec in context.MailingRecords
    
    		 where Convert.ToInt32(mailRec.RecordId) == 1
    
    		 select mailRec;
    
    
    
    	MailingRecord testRec = testUpdate.FirstOrDefault();
    
    
    
    	if (testRec.RecordId != null)
    
    		testRec.AddressBlock = (new Guid()).ToString();
    
    
    
    	System.Data.Linq.ModifiedMemberInfo[] nfo;
    
    
    
    	if (testRec != null)
    
    		nfo = context.MailingRecords.GetModifiedMembers(testRec);  // error
    
    	
    
    	// ...
    
    }
    
    
     
    The MailingEntityContext and MailingRecord classes were generated using sqlmetal (/map from a dbml).

    _WriteMapSource is an instance of XmlMappingSource.
     
    From the .map - 
    <?xml version="1.0" encoding="utf-8"?>
    <Database Name="Automation" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
        <Table Name="dbo.MailingTestRecords" Member="MailingRecords">
            <Type Name="MailingRecord">
                <Column Name="RecordId" Member="RecordId" Storage="_RecordId" DbType="NVarChar(25) NOT NULL" CanBeNull="false" IsPrimaryKey="true" />
                <Column Name="AddressBlock" Member="AddressBlock" Storage="_AddressBlock" DbType="NVarChar(700)" />
                ...
            </Type>
        </Table>
    </Database>
    • Edited by G-Tim Saturday, March 20, 2010 12:12 AM Add XML map segment
    Friday, March 19, 2010 6:00 PM

Answers

  • Hello Tim,

     

    After some further research, I finally find where the issue is.   It should not be a product issue.   Instead, the problem is caused by missing primary key in the external XML mapping file.   Without the key, the class cannot be treated as a LINQ to SQL entity.  

     

    After I used such an XML mapping file, the application is working fine now:

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

    <?xml version="1.0" encoding="utf-8"?>

    <Database Name="Automation" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">

      <Table Name="dbo.MailingTestRecords" Member="MailingRecords">

        <Type Name="MailingRecord">

          <Column Name="RecordId" Member="RecordId" Storage="_RecordId" DbType="NVarChar(25) NOT NULL" CanBeNull="false" IsPrimaryKey="true" />

          <Column Name="AddressBlock" Member="AddressBlock" Storage="_AddressBlock" DbType="NVarChar(700)" />

          <Column Name="AddressLine1" Member="AddressLine1" Storage="_AddressLine1" DbType="NVarChar(65)" />

          <Column Name="AddressLine2" Member="AddressLine2" Storage="_AddressLine2" DbType="NVarChar(65)" />

    ….

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

     

    If you have any questions, please feel free to let me know.

     

    Have a great 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.
    • Marked as answer by G-Tim Wednesday, April 7, 2010 3:13 PM
    Wednesday, April 7, 2010 9:07 AM
    Moderator

All replies

  • Hello,

     

    Welcome to LINQ to SQL forum!

     

    Could you please tell us your Visual Studio version?   Are you using VS2008 or VS2010 RC or Beta?   I did not repro such a problem at my lab.  Would you mind sending me a demo project and db file to repro this issue, if it is convenient for you?    My mail address is v-micsun@microsoft.com.   Thanks a lot!

     

    Have a nice weekend!

     

     

    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.
    Saturday, March 20, 2010 5:43 AM
    Moderator
  • Hello,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know how is the problem now? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    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, March 24, 2010 1:13 AM
    Moderator
  • Hi,

    Sorry for the delay.  I  have just sent a VS2008 solution, which demonstrates the problem, to the email address you mentioned above.

    Due to dependencies (for example access to a Satori MailRoom server) among other things, I am unable to send the original solutions involved.  More detail in the email.

    Thank you!
    Tim

    Wednesday, March 24, 2010 4:18 PM
  • An FYI / Update for anyone else who might be seeing this problem...

    They were able to reproduce the error.

    Circumstances:

    Using XDocument to dynamically provide XML to XmlMappingSource

     

    In the sample above when context.MailingRecords.GetModifiedMembers(testRec) is called -

    from "MetaType metaType = this.Context.Mapping.GetMetaType(entity.GetType());"

    the resulting metaType property IsEntity is false...

     

    Using AttributeMappingSource instead of XmlMappingSource avoids the problem.

     

    They're still looking at it ("I will consult the product team to see whether it is a bug...") and I'm looking at alternative ways to get a dynamic MappingSource.

    Tuesday, March 30, 2010 6:52 PM
  • Hello Tim,

     

    After some further research, I finally find where the issue is.   It should not be a product issue.   Instead, the problem is caused by missing primary key in the external XML mapping file.   Without the key, the class cannot be treated as a LINQ to SQL entity.  

     

    After I used such an XML mapping file, the application is working fine now:

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

    <?xml version="1.0" encoding="utf-8"?>

    <Database Name="Automation" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">

      <Table Name="dbo.MailingTestRecords" Member="MailingRecords">

        <Type Name="MailingRecord">

          <Column Name="RecordId" Member="RecordId" Storage="_RecordId" DbType="NVarChar(25) NOT NULL" CanBeNull="false" IsPrimaryKey="true" />

          <Column Name="AddressBlock" Member="AddressBlock" Storage="_AddressBlock" DbType="NVarChar(700)" />

          <Column Name="AddressLine1" Member="AddressLine1" Storage="_AddressLine1" DbType="NVarChar(65)" />

          <Column Name="AddressLine2" Member="AddressLine2" Storage="_AddressLine2" DbType="NVarChar(65)" />

    ….

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

     

    If you have any questions, please feel free to let me know.

     

    Have a great 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.
    • Marked as answer by G-Tim Wednesday, April 7, 2010 3:13 PM
    Wednesday, April 7, 2010 9:07 AM
    Moderator
  • Thank you!

    Knowing that, it took all of five minutes to find and fix the bug in the XML generation code.

    Linq2Sql POCO entity classes against dynamic table names... really useful in our environment.

    Wednesday, April 7, 2010 3:37 PM
  • You are welcome, Tim!

     

    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.
    Thursday, April 8, 2010 1:09 AM
    Moderator