bubibubi / JetEntityFrameworkProvider

Microsoft Access (Jet) Entity Framework provider
89 stars 26 forks source link

Error when trying to insert (Microsoft.Jet.OLEDB.4.0) #38

Closed damienmartel2 closed 5 years ago

damienmartel2 commented 5 years ago

I am trying to use your provider to work with access database (version Microsoft.Jet.OLEDB.4.0). Select and update operations work very well but when i try to insert an element i have an error when i call the SaveChanges() methods...

the error is : Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

The mdb database is a model with existing tables and no records...

Could you help me ?

                using (var mdbContext = new MdbContext(MDBTools.GetMdbConnectionString(tmpMdbPath)))
                {
                    var bac = new BacEntity();
                    mdbContext.BacEntities.Add(bac);

                    mdbContext.SaveChanges();
                }
    [Table("Bac")]
    public class BacEntity
    {
        [Key]
        [Column("idBac")]
        public int IdBac { get; set; }
        [Column("idScanner")]
        public int IdScanner { get; set; }
        [Column("bacNo")]
        public short BacNo { get; set; }
    }
bubibubi commented 5 years ago

Is the column idBac an autonumbering column? As you defined it on the database should be autonumbering.

You can also enable debugging (Jet provider writes the queries befor run them). The statement to enable debugging should be JetConnection.ShowSqlStatements = true (remember to disable it, otherwise the application could be very slow).

You can also try to create a new DB using standard migrations (if you don't disable it, EF creates the database on first access).

Try also to compact the database.

damienmartel2 commented 5 years ago

First thank you for your help ;)

The column idbac is not autonumbering but if i remove the key attribute i have an error when the context is loaded...

damienmartel2 commented 5 years ago

JetConnection.ShowSqlStatements = true

i add this code but how to track queries ?

bubibubi commented 5 years ago

JetConnection.ShowSqlStatements = true

i add this code but how to track queries ?

The queries are tracked on the console. You need to redirect it if you are using a web server

bubibubi commented 5 years ago

First thank you for your help ;)

The column idbac is not autonumbering but if i remove the key attribute i have an error when the context is loaded...

You are welcome! With this configuration you need to have the idbac column autonumbering. If you don't want to have an autonumbering column, You can set the column not to be database generated (DatabaseGenerated attribute). In this case you need to set bac.IdBac = x before saving changes.

The issue is not really related to the provider. You have the same problem using SQL. You probably could have better support on stack overflow

damienmartel2 commented 5 years ago

the request tracking

ExecuteDbDataReader========== insert into [Bac]([idScanner], [bacNo]) values (@p0, @p1); select [idBac] from [Bac] where [idBac] = @@identity @p0 = 0 @p1 = 0

damienmartel2 commented 5 years ago

It works ! Just i add the attribute ;)

A big thank you for your help ! Good day to you ;)

    [Table("Bac")]
    public class BacEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Column("idBac")]
        public int IdBac { get; set; }
        [Column("idScanner")]
        public int IdScanner { get; set; }
        [Column("bacNo")]
        public short BacNo { get; set; }
    }