我如何限制数据库中的行数为1?

问题描述:

I have a table named profit model:我如何限制数据库中的行数为1?

我想活动不同的profitmodel,表中的数据将被更新。

现在我正在使用UpdateAsync,但它不工作......我该如何实现这一目标?

async void Active_Clicked(object sender, System.EventArgs e) 
    { 
     var profitmodel = (sender as Button).CommandParameter as ProfitModel; 

     await conn.CreateTableAsync<ProfitModelInUsed>(); 

     //System.Diagnostics.Debug.WriteLine(product.ProductName); 

     var profitmodelInUsed = new ProfitModelInUsed 
     { 
      ProfitModel_ID = profitmodel.ProfitModel_ID, 
      ProfitModel_Name = profitmodel.ProfitModel_Name, 
      ExchangeRate = profitmodel.ExchangeRate, 
      Profit = profitmodel.Profit 
     }; 

     await conn.UpdateAsync(profitmodelInUsed); 

     await DisplayAlert("This ProfitModel is Applied", profitmodelInUsed.ProfitModel_Name, "OK"); 
    } 

而且我不想在此表中有多行。

套用PrimaryKeyProfitModel_ID财产的ProfitModelInUsed模型中:

public class ProfitModelInUsed 
{ 
    [PrimaryKey] 
    public string ProfitModel_ID { get; set; } 
    // ~~~ other properties 
} 

而且使用设置ProfitModel_ID属性const

profitmodelInUsed.ProfitModel_ID = "InUseProfitModel" 
await conn.UpdateAsync(profitmodelInUsed); 

通过Retrive它:

var profitmodelinuse = conn.FindAsync<ProfitModelInUsed>("InUseProfitModel"); 
+0

谢谢兄弟!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !你的建议是有帮助的!!!!!!!!!! –