dotConnect SQLite & EF: Autoincrement Primary Key Values?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
ClockEndGooner
Posts: 2
Joined: Sat 11 Jun 2011 15:44
Location: Eastern United States

dotConnect SQLite & EF: Autoincrement Primary Key Values?

Post by ClockEndGooner » Wed 15 Jun 2011 03:38

Greetings;

I'm in the process of evaluating the dotConnect for SQLite Professional Version 3.30.165.0 (Trial License) with Visual Studio 2010 Professional with SP1 applied, and have written a simple console application that attempts to add a series of objects to the tables in my Entity Framework model, which was generated using the Model First approach.

Following the example in your Entity Framework Tutorial, specifically, the section entitled "Inserting New Data", I was able to create a simple C# .NET Console application that inserted the correct record values for each of the entities instantiated, and load the same entity values in the correct order using LINQ to Entity.

Although fairly straight-forward, to manually define each new entity instance's primary key value is not optimal. With this in mind, I am trying to leverage SQLite's ability to define and use Autoincrement Primary Key values. In short, I would like to be able to instantiate new entities and insert them into the database without having to explicitly know and define the new entity instance's primary key value before making and saving the changes through the ObjectContext subclass for my model.

With this in mind, can I let SQLite assign Primary Key values that are defined as Autoincrement and have entity instances inserted into the database where the model, the dotConnect SQLite ADO.NET provider, and the model generated entity classes can recognize and work with SQLite assigned primary keys? If so, how can this be done? Or, does the dotConnect SQLite provider only work with Entity Framework to add new entity instances to the database in which the Primary Key is explicitly defined when the model entity is instantiated for the first time?

Using the C# fragment below, can the Author, Genre, Book entity instances be added to the SQLite database genreated from the Entity Framework model if their respective primary keys are defined as Autoincrement?

Code: Select all

using (var context = new BookEntities()) 
{
    var author = new Author() 
    { FirstName = "Tim", LastName = "Dorsey" };

    var genre = new Genre() { Category = "Fiction" };

    var book1 = new Book() 
    { Title = "Stingray Shuffle", YearPublished = 2003 };

    author.Books.Add(book1);
    genre.Books.Add(book1);

    context.Authors.AddObject(author);
    context.Genres.AddObject(genre);
    context.Books.AddObject(book1);

   context.SaveChanges();
}
Can this approach work? If so, how?

Thank you in advance for your time and help.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Wed 15 Jun 2011 12:33

If you are using the Devart Entity model (.edml) then the solution is to open the model, go to the Model Explorer, open the Model.Store->Tables/Views node, find the necessary table, and set the StoreGeneratedPattern attribute to "Identity".
If you are using the Microsoft ADO.NET Entity Data model (.edmx), open the model using any XML Editor and add StoreGeneratedPattern=“Identity” to the necessary property in the SSDL part of the model.
After setting this attribute the value will be fetched after the SaveChanges call for the column.

ameboide
Posts: 3
Joined: Tue 07 Jun 2011 22:05

Post by ameboide » Fri 17 Jun 2011 15:25

AndreyR wrote:If you are using the Microsoft ADO.NET Entity Data model (.edmx), open the model using any XML Editor and add StoreGeneratedPattern=“Identity” to the necessary property in the SSDL part of the model.
I'm doing that and it works, but everytime I make a change in the database and use the "Update model from database" function I lose those changes. Is that normal or am I doing something wrong?
It's particularly annoying because I have some tables that don't use an autoincremental id (1 - 0..1 relations that only work in EF if you use the first table's PK as the second's PK) so just a simple search and replace isn't enough...

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 20 Jun 2011 09:29

Thank you for the report, I have reproduced the issue.
I will let you know about the results of our investigation.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 20 Jun 2011 12:17

Could you please send us the script of one of the tables the problem is reproducible on?
This will speed up our investigation.

ClockEndGooner
Posts: 2
Joined: Sat 11 Jun 2011 15:44
Location: Eastern United States

Re: dotConnect SQLite & EF: Autoincrement Primary Key Values

Post by ClockEndGooner » Mon 27 Jun 2011 03:00

Per your recommendation, I've created and added a new Devart Entity Model for SQLite, and the Store Generated Pattern for each entity's primary key was set to "Identity". My updated model and solution compiled without any warnings or errors, but when executing the following code, I received the following unhandled inner exception when debugging the application at the point the SaveChanges() method is called in the Data Context instance:

Code: Select all

    
InnerException: System.InvalidOperationException
   Message=A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'GenreID'.
   Source=System.Data.Entity
   StackTrace:
        at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding target, PropagatorResult row, PropagatorResult originalRow, TableChangeProcessor processor, Boolean insertMode, Dictionary`2& outputIdentifiers, DbExpression& returning, Boolean& rowMustBeTouched)
        at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor)
        at System.Data.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler)
   InnerException: 
Is there anything else I need to set in the properties for each of the entities or model itself to address this error? Also, please let me know if you need me to post or E-Mail you my sample application and model.

Thank you again for your time, help and patience...

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 27 Jun 2011 15:19

Please send us a small test project illustrating the issue.
The test project will speed up our investigation.

Post Reply