Trouble inserting new object

Trouble inserting new object

Postby msw10100 » Thu 20 Sep 2012 04:30

I'm trying something that looks like it should be simple. I'm not using Entity Developer, just working manually to create an entity that should work against a MySql database.

Code: Select all
    [Table(Name = "document_types")]
    public class DocumentType
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int Id;
        [Column]
        public string Name;
    }

... then, later in a method in another class...

     var dcon = new VnDocsDataContext();
     var dt = new DocumentType();
     dt.Name = "This is a test";
     dcon.DocumentTypes.InsertOnSubmit(dt);
     dcon.SubmitChanges();


When dcon.SubmitChanges() runs, I get an exception:

Code: Select all
An unhandled exception of type 'System.InvalidOperationException' occurred in Devart.Data.Linq.dll

Additional information: The entity ConsoleApplication3.DocumentType should be attached to the context to perform this operation.


What might I be doing incorrectly, here?

Thanks -- Michael Watson
msw10100
 
Posts: 12
Joined: Thu 20 Sep 2012 04:00

Re: Trouble inserting new object

Postby MariiaI » Thu 20 Sep 2012 11:33

Thank you for the report. This is a known issue and we are working on it. We will inform you when it is fixed.
To avoid it, please add the INotifyPropertyChanging interface and PropertyChangingEventHandler to the DocumentType class definition like it described below:
Code: Select all
public class DocumentType: INotifyPropertyChanging
  {
      public event PropertyChangingEventHandler PropertyChanging;

      [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
      public int Id;
      [Column]
      public string Name;
  }

Please notify us if this helps.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Trouble inserting new object

Postby msw10100 » Thu 20 Sep 2012 15:18

Thank you for your response!

Making my entity implement INotifyPropertyChanging resolved the problem for me. I will continue evaluating LinqConnect this morning. We've got a project that I'm hoping will benefit from using LinqConnect.

Thanks again!

-- Michael Watson
msw10100
 
Posts: 12
Joined: Thu 20 Sep 2012 04:00

Re: Trouble inserting new object

Postby MariiaI » Fri 21 Sep 2012 06:12

Glad to see that the problem was resolved. If you have any further questions, feel free to contact us.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Trouble inserting new object

Postby msw10100 » Tue 25 Sep 2012 20:39

Something new popped up. See the entity definition in the earlier post above. Given that definition, I find that queries for individual entities works well for me. However, when I try to return multiple rows, I get an exception. Here's the code I'm running against the DataContext with a DocumentType entity:

Code: Select all
            var query = from it in context.DocumentTypes
                        orderby it.Id
                        select it;
            foreach (DocumentType dt in query)
            {
                Console.WriteLine("{0}", dt.Id);
            }


When I run this, I get the following exception:

Code: Select all
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Destination array is not long enough to copy all the items in the collection. Check array index and length.


Note that there are only a couple records in the table. And I've tried it with as few as 1 record. Again, when I pull this one record with a query for it specifically, it works. When I try to retrieve it with the code above, it gives the exception.

This seems to follow the examples in the LinqConnect tutorials, and I can't see what might be wrong. I get this anytime I try to retrieve more than a single entity.

Thanks -- Michael Watson
msw10100
 
Posts: 12
Joined: Thu 20 Sep 2012 04:00

Re: Trouble inserting new object

Postby MariiaI » Wed 26 Sep 2012 08:40

We couldn't reproduce this issue. We have sent you a sample project. Please check that the letter is not blocked by your mail filter.
Please try the sample and make changes to it, so that the issue could be reproduced, and send it back to us, or send us your project.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Trouble inserting new object

Postby msw10100 » Wed 26 Sep 2012 15:23

Your project worked correctly. I started adding in my other columns and got the same error:

Code: Select all
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Destination array is not long enough to copy all the items in the collection. Check array index and length.


I found and corrected the problem. I had a column defined in MySQL as a BIT, but in my LINQConnect code, I defined it as an "int". This produced the error. When I changed the definition in my code to "bool", the problem went away.

Many thanks for your sample project, which helped me work toward a solution. The error message was not helpful, and it was only by incrementally working from your sample toward mine that I was able to solve it.

Again, thanks!

Cheers -- Michael Watson
msw10100
 
Posts: 12
Joined: Thu 20 Sep 2012 04:00

Re: Trouble inserting new object

Postby MariiaI » Thu 27 Sep 2012 08:33

Glad to see that you found the solution to the problem and thank you for the changed test project. We have reproduced the error when the column is defined in MySQL as a "BIT", while in the code it is defined as an "INT". We will investigate it and inform you about the results.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Trouble inserting new object

Postby MariiaI » Mon 22 Oct 2012 07:01

The bug when working with entities without INotifyPropertyChanging, INotifyPropertyChanged interfaces is fixed. The fix is available in the new build of LinqConnect 4.1.109.
It can be downloaded from http://www.devart.com/linqconnect/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=31&t=25101
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Trouble inserting new object

Postby MariiaI » Wed 24 Oct 2012 12:12

The bug that related to the issue when the column is defined in MySQL as a "BIT", while in the code it is defined as an "INT", has been fixed. The fix will be included in the new build of LinqConnect. We will inform you when the new build is available.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Trouble inserting new object

Postby MariiaI » Mon 05 Nov 2012 07:20

New build of LinqConnect 4.1.119 is available for download now. It can be downloaded from http://www.devart.com/linqconnect/download.html (trial version) or from Registered Users' Area (for users with valid subscription only).
For more information please refer to http://forums.devart.com/viewtopic.php?f=31&t=25201
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17


Return to LinqConnect (LINQ to SQL support)