Microsoft.Data.Sqlite.SqliteException:SQLite错误19:“ FOREIGN KEY约束失败”

时间:2019-07-16 08:31:41

标签: c# entity-framework xamarin.forms

这是完整的异常详细信息

   {Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.Sqlite.SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.
      at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC (System.Int32 rc, SQLitePCL.sqlite3 db) [0x00067] in <b46e232ade014524baa458345f270c50>:0 
      at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) [0x00282] in <b46e232ade014524baa458345f270c50>:0 
      at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync (System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken) [0x00007] in <b46e232ade014524baa458345f270c50>:0 
      at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync (System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken) [0x00011] in <b46e232ade014524baa458345f270c50>:0 
      at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, Microsoft.EntityFrameworkCore.Diagnostics.DbCommandMethod executeMethod, System.Collections.Generic.IReadOnlyDictionary`2[TKey,TValue] parameterValues, System.Threading.CancellationToken cancellationToken) [0x00358] in <69f795dffc844780bfcfff4ff8415a92>:0 
      at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, System.Threading.CancellationToken cancellationToken) [0x000a0] in <69f795dffc844780bfcfff4ff8415a92>:0 
       --- End of inner exception stack trace ---
      at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, System.Threading.CancellationToken cancellationToken) [0x00152] in <69f795dffc844780bfcfff4ff8415a92>:0 
      at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync (Microsoft.EntityFrameworkCore.DbContext _, System.ValueTuple`2[T1,T2] parameters, System.Threading.CancellationToken cancellationToken) [0x00220] in <69f795dffc844780bfcfff4ff8415a92>:0 
      at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync (System.Collections.Generic.IReadOnlyList`1[T] entriesToSave, System.Threading.CancellationToken cancellationToken) [0x00088] in <adf771f92e754fe1bb85c5850cd0c16b>:0 
      at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync (System.Boolean acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken) [0x000f8] in <adf771f92e754fe1bb85c5850cd0c16b>:0 
      at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync (System.Boolean acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken) [0x000e9] in <adf771f92e754fe1bb85c5850cd0c16b>:0 
      at Mobile.ViewModel.OrdersDetailViewModel.DatabaseAttachmentOpertaions () [0x00369]

有时会产生此异常

{Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.Sqlite.SqliteException: SQLite Error 19: 'UNIQUE constraint failed: fileAttachments._id'.
  at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC (System.Int32 rc, SQLitePCL.sqlite3 db) [0x00067] in <b46e232ade014524baa458345f270c50>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) [0x00282] in <b46e232ade014524baa458345f270c50>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync (System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken) [0x00007] in <b46e232ade014524baa458345f270c50>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync (System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken) [0x00011] in <b46e232ade014524baa458345f270c50>:0 
  at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, Microsoft.EntityFrameworkCore.Diagnostics.DbCommandMethod executeMethod, System.Collections.Generic.IReadOnlyDictionary`2[TKey,TValue] parameterValues, System.Threading.CancellationToken cancellationToken) [0x00358] in <69f795dffc844780bfcfff4ff8415a92>:0 
  at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, System.Threading.CancellationToken cancellationToken) [0x000a0] in <69f795dffc844780bfcfff4ff8415a92>:0 
   --- End of inner exception stack trace ---
  at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, System.Threading.CancellationToken cancellationToken) [0x00152] in <69f795dffc844780bfcfff4ff8415a92>:0 
  at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync (Microsoft.EntityFrameworkCore.DbContext _, System.ValueTuple`2[T1,T2] parameters, System.Threading.CancellationToken cancellationToken) [0x00220] in <69f795dffc844780bfcfff4ff8415a92>:0 
  at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync (System.Collections.Generic.IReadOnlyList`1[T] entriesToSave, System.Threading.CancellationToken cancellationToken) [0x00088] in <adf771f92e754fe1bb85c5850cd0c16b>:0 
  at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync (System.Boolean acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken) [0x000f8] in <adf771f92e754fe1bb85c5850cd0c16b>:0 
  at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync (System.Boolean acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken) [0x000e9] in <adf771f92e754fe1bb85c5850cd0c16b>:0 

我有这两个模型,当我更新模型时,此异常失败。  我上传了picture1,然后我叫saveChanges,然后引发了此异常

  [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid UniqueID { get { return _UniqueID; } set { _UniqueID = value; RaisePropertyChanged(() => UniqueID); } }

        private int _ID = 0;
        public int ID { get { return _ID; } set { _ID = value; RaisePropertyChanged(() => ID); } }

        private string _TreetraceOrderNumber = "";
        public string TreetraceOrderNumber { get { return _TreetraceOrderNumber; } set { _TreetraceOrderNumber = value; RaisePropertyChanged(() => TreetraceOrderNumber); } }

        private DateTime? _TreetraceOrderDate = DateTime.Now;
        public DateTime TreetraceOrderDate { get { return (DateTime)_TreetraceOrderDate; } set { _TreetraceOrderDate = value; RaisePropertyChanged(() => TreetraceOrderDate); } }

        private string _SupplierId = "";
        public string SupplierId { get { return _SupplierId; } set { _SupplierId = value; RaisePropertyChanged(() => SupplierId); } }
        [NotMapped]
        public Supplier Supplier { get; set; }

        private string _Afdeling = "";
        public string Afdeling { get { return _Afdeling; } set { _Afdeling = value; RaisePropertyChanged(() => Afdeling); } }

        private string _TreetraceStatus = "";
        public string TreetraceStatus { get { return _TreetraceStatus; } set { _TreetraceStatus = value; RaisePropertyChanged(() => TreetraceStatus); } }

        private bool _Attachments = false;
        public bool Attachments { get { return _Attachments; } set { _Attachments = value; RaisePropertyChanged(() => Attachments); } }

        private bool _SyncAttachment = false;
        public bool SyncAttachment { get { return _SyncAttachment; } set { _SyncAttachment = value; RaisePropertyChanged(() => SyncAttachment); } }

        private bool? _TreetraceIsIrrelevant = false;
        public bool? TreetraceIsIrrelevant { get { return _TreetraceIsIrrelevant; } set { if (value == null) { _TreetraceIsIrrelevant = false; } else { _TreetraceIsIrrelevant = value; } RaisePropertyChanged(() => TreetraceIsIrrelevant); } }

        private string _TreetraceComments = "";
        public string TreetraceComments { get { return _TreetraceComments; } set { _TreetraceComments = value; RaisePropertyChanged(() => TreetraceComments); } }

        private string _fileName = string.Empty;
        public string fileName { get { return _fileName; } set { _fileName = value; RaisePropertyChanged(() => fileName); } }

        private string _fileNumber = string.Empty;
        public string fileNumber { get { return _fileNumber; } set { _fileNumber = value; RaisePropertyChanged(() => fileNumber); } }

        private string _TreetraceHighestStatusLine = "";
        public string TreetraceHighestStatusLine { get { return _TreetraceHighestStatusLine; } set { _TreetraceHighestStatusLine = value; RaisePropertyChanged(() => TreetraceHighestStatusLine); } }

        private string _TreeTraceSupplierName = "";
        public string TreeTraceSupplierName { get { return _TreeTraceSupplierName; } set { _TreeTraceSupplierName = value; RaisePropertyChanged(() => TreeTraceSupplierName); } }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        private string _UpdatedByApp = string.Empty;
        public string UpdatedByApp { get { return _UpdatedByApp; } set { _UpdatedByApp = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }
        private string _UpdatedByOS = string.Empty;
        public string UpdatedByOS { get { return _UpdatedByOS; } set { _UpdatedByOS = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdatedByOSVersion = string.Empty;
        public string UpdatedByOSVersion { get { return _UpdatedByOSVersion; } set { _UpdatedByOSVersion = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdateByAppVersionName = string.Empty;
        public string UpdateByAppVersionName { get { return _UpdateByAppVersionName; } set { _UpdateByAppVersionName = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdateByAppVersionNumber = string.Empty;
        public string UpdateByAppVersionNumber { get { return _UpdateByAppVersionNumber; } set { _UpdateByAppVersionNumber = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdatedByNetworkType = string.Empty;
        public string UpdatedByNetworkType { get { return _UpdatedByNetworkType; } set { _UpdatedByNetworkType = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdatedByManufacturer = string.Empty;
        public string UpdatedByManufacturer { get { return _UpdatedByManufacturer; } set { _UpdatedByManufacturer = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdatedByModel = string.Empty;
        public string UpdatedByModel { get { return _UpdatedByModel; } set { _UpdatedByModel = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _DeviceTypeCreatedBy = string.Empty;
        public string DeviceTypeCreatedBy { get { return _DeviceTypeCreatedBy; } set { _DeviceTypeCreatedBy = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _DeviceTypeUpdatedBy = string.Empty;
        public string DeviceTypeUpdatedBy { get { return _DeviceTypeUpdatedBy; } set { _DeviceTypeUpdatedBy = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdatedLongitude = string.Empty;
        public string UpdatedLongitude { get { return _UpdatedLongitude; } set { _UpdatedLongitude = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }

        private string _UpdatedLatitude = string.Empty;
        public string UpdatedLatitude { get { return _UpdatedLatitude; } set { _UpdatedLatitude = value; /*RaisePropertyChanged(() => SGDDDepRegion);*/ } }


        //private string _Picture1Blobbed = "";
        //public string Picture1Blobbed { get { return _Picture1Blobbed; } set { _Picture1Blobbed = value; RaisePropertyChanged(() => Picture1Blobbed); } }
        //private string _Picture2Blobbed = "";
        //public string Picture2Blobbed { get { return _Picture2Blobbed; } set { _Picture2Blobbed = value; RaisePropertyChanged(() => Picture2Blobbed); } }
        //private string _Picture3Blobbed = "";
        //public string Picture3Blobbed { get { return _Picture3Blobbed; } set { _Picture3Blobbed = value; RaisePropertyChanged(() => Picture3Blobbed); } }
        //private string _Picture4Blobbed = "";
        //public string Picture4Blobbed { get { return _Picture4Blobbed; } set { _Picture4Blobbed = value; RaisePropertyChanged(() => Picture4Blobbed); } }

        [NotMapped]

        public List<FileAttachment> OtherDocuments { get; set; }


        //private FileAttachment _fileAttachment = null;
        ////[OneToMany]
        //public FileAttachment FileAttachment { get { return _fileAttachment; } set { _fileAttachment = value; RaisePropertyChanged(() => FileAttachment); } }

        private List<FileAttachment> _fileAttachments = null;
        [InverseProperty("ordersModel")]
        public List<FileAttachment> FileAttachments { get { return _fileAttachments; } set { _fileAttachments = value; RaisePropertyChanged(() => FileAttachments); } }

        private FileAttachment _Picture1 = null;
        [NotMapped]
        public FileAttachment Picture1 { get { return _Picture1; } set { _Picture1 = value; } }

        private FileAttachment _Picture2 = null;
        [NotMapped]
        public FileAttachment Picture2 { get { return _Picture2; } set { _Picture2 = value; } }

        private FileAttachment _Picture3 = null;
        [NotMapped]
        public FileAttachment Picture3 { get { return _Picture3; } set { _Picture3 = value; } }

        private FileAttachment _Picture4 = null;
        [NotMapped]
        public FileAttachment Picture4 { get { return _Picture4; } set { _Picture4 = value; } }

        private FileAttachment _compressedPicture1 = null;
        [NotMapped]
        public FileAttachment CompressedPicture1 { get { return _compressedPicture1; } set { _compressedPicture1 = value; RaisePropertyChanged(() => CompressedPicture1); } }

        private FileAttachment _compressedPicture2 = null;
        [NotMapped]
        public FileAttachment CompressedPicture2 { get { return _compressedPicture2; } set { _compressedPicture2 = value; RaisePropertyChanged(() => CompressedPicture2); } }

        private FileAttachment _compressedPicture3 = null;
        [NotMapped]
        public FileAttachment CompressedPicture3 { get { return _compressedPicture3; } set { _compressedPicture3 = value; RaisePropertyChanged(() => CompressedPicture3); } }

        private FileAttachment _compressedPicture4 = null;
        [NotMapped]
        public FileAttachment CompressedPicture4 { get { return _compressedPicture4; } set { _compressedPicture4 = value; RaisePropertyChanged(() => CompressedPicture4); } }

这是第二个模型,我没有发现我做错的问题。我有使用Foreignkey和Inverse属性来使它们之间建立关系

[Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int _id { get; set; }
        public string filename { get; set; }
        public byte[] base64 { get; set; }
        public long filesize { get; set; }
        public string filetype { get; set; }
        //[Ignore]
        //public MediaFile stream { get; set; }

        private Guid _OrderID = Guid.NewGuid();
        [ForeignKey("OrderID")]
        public Guid OrderID { get { return _OrderID; } set { _OrderID = value; RaisePropertyChanged(() => OrderID); } }
        public OrdersModel  ordersModel { get; set; }


        private string _TreetraceOrderNumber = "";
        public string TreetraceOrderNumber { get { return _TreetraceOrderNumber; } set { _TreetraceOrderNumber = value; RaisePropertyChanged(() => TreetraceOrderNumber); } }

这里我正在更新表格。

List<Model.FileAttachment> itemsToDelete =  App.dbContext.fileAttachments.Where(x => x.OrderID == Model.UniqueID || x.TreetraceOrderNumber == Model.TreetraceOrderNumber).ToList();
                if (itemsToDelete.Count > 0)
                {
                    App.dbContext.fileAttachments.RemoveRange(itemsToDelete);
                    await App.dbContext.SaveChangesAsync();
                }
                    //await App.dbContext.DeleteAllAsync(itemsToDelete);
                if (Model.TreetraceStatus == Config.CompletedStatus && Model.FileAttachments != null && Model.FileAttachments.Count != 0)
                {
                    foreach (var OrdrNr in Model.FileAttachments)
                    {
                        OrdrNr.TreetraceOrderNumber = Model.TreetraceOrderNumber;

                    }
                    App.dbContext.fileAttachments.UpdateRange(Model.FileAttachments);
                    await App.dbContext.SaveChangesAsync();

                    var tracking = App.dbContext.ordersModels.Update(Model);

                    await App.dbContext.SaveChangesAsync();
                    var isModified = tracking.State == EntityState.Modified;

0 个答案:

没有答案