如何在数据库中将行限制为一个?

时间:2017-09-25 01:32:01

标签: xamarin sqlite xamarin.forms

I have a table named profit model:

我想激活不同的利润模型,表格中的数据将会更新。

现在我正在使用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");
    }

我不希望在此表中有多行。

1 个答案:

答案 0 :(得分:0)

PrimaryKey应用于ProfitModel_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);

通过以下方式回复:

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