如何从子记录中删除外键约束?

时间:2018-06-06 14:41:08

标签: c# sqlite uwp azure-sql-database azure-mobile-services

我遇到了以下问题:我有两个表,一个是OverTimeRecord表,用于存储有关员工长期工作的信息,这是我在天蓝色后端的课程:

 public class OverTimeRecord : EntityData
 {
    [Required]
    [Column(TypeName = "datetime2")]
    public DateTime? OverTimeWorkedDate { get; set; }
    [Required]
    [Column(TypeName ="time")]
    public TimeSpan? BeginTime { get; set; }
    [Required]
    [Column(TypeName ="time")]
    public TimeSpan? EndTime { get; set; }
    [Required]
    public double? WorkedHours { get; set; }
    [Required]
    public double? DoubleValueHours { get; set; }
    [Required]
    public double? TripleValueHours { get; set; }
    [Required]
    public PaymentFactor Factor { get; set; }
    [Required]
    public PaidStatus PaymentStatus { get; set; }
    [Required]
    public PaymentTime IsOnTime { get; set; }
    [Required]
    public Fortnight FortnightPayment { get; set; }
    public string UserId { get; set; }

    #region Relathionship with OverTimeFortnight
    public string OverTimeFortnightId { get; set; }

    [ForeignKey(nameof(OverTimeFortnightId))]
    public OverTimeFortnight FortnightInfo { get; set; }
    #endregion
}

这与父母OverTimeFortnight有关系,这是记录所有希望双周加班的记录,这是我的后端课程:

public class OverTimeFortnight : EntityData
{
    [Required]
    [Column(TypeName ="datetime2")]
    public DateTime? PaymentDate { get; set; }
    [Required]
    [Column(TypeName = "datetime2")]
    public DateTime? FortnightStart { get; set; }
    [Required]
    [Column(TypeName = "datetime2")]
    public DateTime? FortnightEnd { get; set; }
    [Required]
    public decimal? CurrentBasicSalary { get; set; }
    [Required]
    public bool? IncludeDelayed { get; set; }
    [Required]
    public Fortnight FortnightNumber { get; set; }
    public string UserId { get; set; }
}

问题是,当我尝试更新加班记录时,我说我试图通过删除OvertimeFortnight来删除与OverTimeFortnightId的关系,Id与{{1}保持直接引用来自OverTimeFortnight类我收到一个错误,具体说我有一个FK约束。

现在如果我删除Overtimefortnight Record我可以更新任何OverTimeRecord或者如果我删除OverTimeRecord,我可以使用这种方法作为解决方法:

  1. 将OverTimeRecord存储在临时。
  2. 删除原始OverTimeRecord。
  3. 在我的客户端,因为它使用SQL Lite的脱机支持,PushAsync更改为与云同步。
  4. 使用临时变量并使用OvertimeFortnightId创建一个新的OverTimeRecord为空。
  5. 再次将PushAsync更改为与云同步。
  6. 只是为了删除存储在OverTimeFortnightId中的字符串,有没有办法能够删除OverTimeFortnightId并更新OverTimeRecord类而不触发FK约束?

    我确信我做错了但我不知道在哪里寻找或如何解决这个具体问题。

    我将分享我的客户类实体:

     public class OverTimeRecord : TableData
    {
        [Required]
        [Column(TypeName = "datetime2")]
        [JsonProperty(PropertyName ="overtimeworkeddate")]
        public DateTime? OverTimeWorkedDate { get; set; }
        [Required]
        [Column(TypeName = "time")]
        [JsonProperty(PropertyName ="begintime")]
        public TimeSpan? BeginTime { get; set; }
        [Required]
        [Column(TypeName = "time")]
        [JsonProperty(PropertyName ="endtime")]
        public TimeSpan? EndTime { get; set; }
        [Required]
        [JsonProperty(PropertyName ="workedhours")]
        public double? WorkedHours { get; set; }
        [Required]
        [JsonProperty(PropertyName ="doublevaluehours")]
        public double? DoubleValueHours { get; set; }
        [Required]
        [JsonProperty(PropertyName ="triplevaluehours")]
        public double? TripleValueHours { get; set; }
        [Required]
        [JsonProperty(PropertyName ="factor")]
        public PaymentFactor Factor { get; set; }
        [Required]
        [JsonProperty(PropertyName ="paymentstatus")]
        public PaidStatus PaymentStatus { get; set; }
        [Required]
        [JsonProperty(PropertyName ="isontime")]
        public PaymentTime IsOnTime { get; set; }
        [Required]
        [JsonProperty(PropertyName ="fortnightpayment")]
        public Fortnight FortnightPayment { get; set; }
        [JsonProperty(PropertyName ="userid")]
        public string UserId { get; set; }
        [JsonProperty(PropertyName ="overtimefortnightid")]
        public string OverTimeFortnightId { get; set; }
    
        public override bool Equals(object obj)
        {
            return Equals<OverTimeRecord>(obj, this);
        }
    
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
    
     public class OverTimeFortnight : TableData
    {
        [Required]
        [Column(TypeName = "datetime2")]
        [JsonProperty(PropertyName ="paymentdate")]
        public DateTime? PaymentDate { get; set; }
        [Required]
        [Column(TypeName = "datetime2")]
        [JsonProperty(PropertyName = "fortnightstart")]
        public DateTime? FortnightStart { get; set; }
        [Required]
        [Column(TypeName = "datetime2")]
        [JsonProperty(PropertyName = "fortnightend")]
        public DateTime? FortnightEnd { get; set; }
        [Required]
        [JsonProperty(PropertyName = "currentbasicsalary")]
        public decimal? CurrentBasicSalary { get; set; }
        [Required]
        [JsonProperty(PropertyName ="includedelayed")]
        public bool? IncludeDelayed { get; set; }
        [Required]
        [JsonProperty(PropertyName = "fortnightnumber")]
        public Fortnight FortnightNumber { get; set; }
        [JsonProperty(PropertyName = "userid")]
        public string UserId { get; set; }
    
        public override bool Equals(object obj)
        {
            return Equals<OverTimeFortnight>(obj, this);
        }
    
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
    

    这是更新工作的方法,但我发现它会降低我的应用程序的性能,以防我需要从OverTimeFortnight取消链接许多不同的OverTimeRecords。

    async Task ExecuteUpdateOverTimeFortnightCommand()
        {
            SetBusy(IsBusy, UpdatingOverTimeFortnightWaitMessage);
            try
            {
                Debug.WriteLine($"[OverTimeFortnightRegisterPageViewModel] - Saving information into database");
                OverTimeFortnightInfo.UpdateOverTimeFortnight(OverTimeFortnightItem);
                await TableFortnights.UpsertItemAsync(OverTimeFortnightItem);
                IsSuccessful = true;
                Debug.WriteLine($"[OverTimeFortnightRegisterPageViewModel] - Saved successful on database");
                await SynchronizeDatabaseAsync("[OverTimeFortnightRegisterPageViewModel] - ");
                await RefreshListsAsync();
                await UpdateOverTimeRecordsAsync(OverTimeFortnightItem);
            }
            catch (MobileServicePreconditionFailedException<OverTimeFortnight> ex)
            {
                await ResolvePreconditionConflictAsync(OverTimeFortnightInfo.Clone(), ex, TableFortnights);
            }
            catch (Exception ex)
            {
                IsSuccessful = false;
                Debug.Write($"[OverTimeFortnightRegisterPageViewModel] - Error at storing fortnight: {ex.Message}");
                await ShowMessageDialogAsync(UpdatingOverTimeFortnightFailedMessage);
            }
            finally
            {
                SetBusy(!IsBusy);
    
                if (IsSuccessful)
                    await ShowMessageDialogAsync(UpdatingOverTimeFortnightSuccessfulMessage);
    
                IsSuccessful = true;
                IsUnLock = false;
                HasListRecords = false;
    
                NavigationService.GoBack();
            }            
        }
    
    async Task UpdateOverTimeRecordsAsync(OverTimeFortnight otf)
        {
            foreach (var rot in OverTimeRecords)
            {
                if (string.IsNullOrEmpty(rot.OverTimeFortnightId))
                {
                    await UpdateOverTimeRecordAsync(rot, otf);
                }
                else if (!rot.OverTimeFortnightId.Equals(otf.Id))
                {
                    await UpdateOverTimeRecordAsync(rot, otf);
                }
            }
    
            if (DumpOverTimesRecords.Count() > 0)
            {
                foreach (var rot in DumpOverTimesRecords)
                {
                    var rotTemp = rot;
                    await TableOverTimes.DeleteItemAsync(rot);
                    await SynchronizeDatabaseAsync("[OverTimeFortnightRegisterPageViewModel] - ");
                    await UnLinkOverTimeRecordAsync(rotTemp);
                    await SynchronizeDatabaseAsync("[OverTimeFortnightRegisterPageViewModel] - ");
                }
            }
    
            DumpOverTimesRecords.Clear();
            await SynchronizeDatabaseAsync("[OverTimeFortnightRegisterPageViewModel] - ");
            await RefreshListsAsync();
        }
    
    async Task UnLinkOverTimeRecordAsync(OverTimeRecord rot)
        {
            rot.PaymentStatus = PaidStatus.UnPaid;
            rot.FortnightPayment = Fortnight.Q00;
            await TableOverTimes.UpsertItemAsync(new OverTimeRecord
            {
                OverTimeWorkedDate = rot.OverTimeWorkedDate,
                Factor=rot.Factor,
                IsOnTime=rot.IsOnTime,
                DoubleValueHours=rot.DoubleValueHours,
                TripleValueHours=rot.TripleValueHours,
                WorkedHours=rot.WorkedHours,
                PaymentStatus=rot.PaymentStatus,
                FortnightPayment=rot.FortnightPayment,
                BeginTime = rot.BeginTime,
                EndTime=rot.EndTime
            });
        }
    

    我希望有人可以给我一个更好的建议,感谢那些试图帮助我的人!

0 个答案:

没有答案