[英]Remove the selected item in a ListView from a SQL database
我試圖創建一種從數據庫表mrydenEntities()
中刪除我選擇的項(從列表視圖中刪除)的方法,並使用EntityFramework
和Database First模型設置了數據庫。
我收到這個錯誤
System.InvalidOperationException:序列不包含任何元素
我可以肯定地說這意味着我的代碼recipeToRemove = context.Recipe.First(r => r.Id.Equals(Id));
沒有得到任何相等的比賽。
我不確定我做錯了什么,我在選票上用盡了如何前進。
我的Db類:
public class Dbconnection : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string RecipeName { get; set; }
public string RecipeIngredients { get; set; }
public string CookingTime { get; set; }
public int Id { get; set; }
mrydendbEntities context = new mrydendbEntities();
public Recipe recipeToRemove;
public Recipe Table { get; set; }
public void DbconnectionRemove(string RecipeName, string Ingredients, string CookingTime, int Id)
{
recipeToRemove = context.Recipe.First(r => r.Id.Equals(Id)); //This is giving me an error System.InvalidOperationException: Sequence contains no elements
context.Recipe.Remove(recipeToRemove);
context.SaveChanges();
MessageBox.Show(recipeToRemove.RecipeName);
}
}
我的代碼背后:
public partial class ViewAll : Page, INotifyPropertyChanged
{
public int Id { get; set; }
public string RecipeName { get; set; }
public string Ingredients { get; set; }
public string CookingTime { get; set; }
private Recipe _recipe;
public Recipe Recipe
{
get { return _recipe; }
set
{
if(_recipe != value)
{
_recipe = value;
OnPropertyChanged("Recipe");
}
}
}
public ViewAll()
{
InitializeComponent();
LoadItemTemplate();
}
public void LoadItemTemplate()
{
mrydendbEntities dbe = new mrydendbEntities();
listOfRecipes.ItemsSource = dbe.Recipe.ToList();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void Button_Click(object sender, RoutedEventArgs e) //My button event to remove the selected table row.
{
var remove = new Dbconnection();
remove.DbconnectionRemove(RecipeName, Ingredients, CookingTime, Id);
}
我的mrydendbEntities類:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class mrydendbEntities : DbContext
{
public mrydendbEntities()
: base("name=mrydendbEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Recipe> Recipe { get; set; }
}
編輯:更改了recipeToRemove = context.Recipe.First(r => r.Id.Equals(Id));
替換為recipeToRemove = context.Recipe.FirstOrDefault(r => r.RecipeName.Equals(RecipeName));
這在行context.Recipe.Remove(recipeToRemove);
給了我一個新的錯誤消息context.Recipe.Remove(recipeToRemove);
System.ArgumentNullException“值不能為空”
代替這個:
recipeToRemove = context.Recipe.First(r => r.Id.Equals(Id)); //This is giving me an error System.InvalidOperationException: Sequence contains no elements
做這個:
recipeToRemove = context.Recipe.FirstOrDefault(r => r.Id.Equals(Id));
然后在繼續之前檢查結果是否為<null>
。
如果沒有結果,則First
將出錯,但是如果沒有結果,則FirstOrDefault
將返回<null>
。
我解決了
我必須將recipeToRemove = context.Recipe.FirstOrDefault(r => r.RecipeName.Equals(RecipeName))
更改為recipeToRemove = context.Recipe.First(r => r.RecipeName.Equals(r.RecipeName));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.