Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First time using Entity Framework 6 with WPF.I created a sample database to get familiar with WPF and Entity Framework.
How do I delete a selected entity

This is my code

namespace RubyTime.Pages
{

public partial class Home : UserControl
{
private RubyTime.RubyEntities ruby;
private System.Windows.Data.CollectionViewSource employeeViewSource;

public Home()
{
InitializeComponent();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{

ruby = new RubyTime.RubyEntities();
employeeViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeeViewSource")));
ruby.Employees.Load();
employeeViewSource.Source = ruby.Employees.Local;


}

private void btnPrevious_Click(object sender, RoutedEventArgs e)
{
if (employeeViewSource.View.CurrentPosition > 0)
employeeViewSource.View.MoveCurrentToPrevious();
}

private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (employeeViewSource.View.CurrentPosition < ((CollectionView)employeeViewSource.View).Count - 1)
employeeViewSource.View.MoveCurrentToNext();
}

private void btnNew_Click(object sender, RoutedEventArgs e)
{
ruby.Employees.Add(new Employee());
employeeViewSource.View.Refresh();
employeeViewSource.View.MoveCurrentToLast();
}

private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
ruby.SaveChanges();
employeeViewSource.View.Refresh();
}


private void btnDelete_Click(object sender, RoutedEventArgs e)
{
Employee employeeToDelete;

using (var ctx = new RubyEntities())
{

employeeToDelete = ctx.Employees.Where(s => s.EmployeeID == "EmployeeID").FirstOrDefault<employee>();

}


using (var newcontext = new RubyEntities())
{


newcontext.Entry(employeeToDelete).State = System.Data.Entity.EntityState.Deleted;
newcontext.SaveChanges();
}


}
}
}

The delete action throws an exception "Operator '==' cannot be applied to operands of type 'int' and 'string'".I know I have to convert EmployeeID to string but I have hit a blank

Much appreciated
gerhard
Posted

1 solution

Um...I don;t think it will help, you will always fail the test.
If s.EmployeeID is an integer, then even if you convert it to a string:
C#
employeeToDelete = ctx.Employees.Where(s => s.EmployeeID.ToString() == "EmployeeID").FirstOrDefault();
it will never match the string "EmployeeID", because it will be a string version of an integer: "1", "2", "3", ...

I think you need to go back to the value you want to compare it against, and work out how to get that as an integer value which represents the actual employee data you need rather than trying to convert each possible ID value to a string.




XML
private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

            using (var emp = new RubyEntities())
            {
                emp.Configuration.AutoDetectChangesEnabled = true;
                var employeeToDelete = emp.Employees.ToList<Employee>();
                emp.Employees.Remove(employeeToDelete.ElementAt<Employee>(0));
                employeeViewSource.View.MoveCurrentToNext();
                emp.SaveChanges();
                employeeViewSource.View.Refresh();

            }


        }
 
Share this answer
 
v2
Comments
Gerhard_Louis 3-Jan-15 5:47am    
Thanks.The holidays were tough on the gray matter.When I saw your answer I realised what I was trying to do which will never work
OriginalGriff 3-Jan-15 6:16am    
:laugh: I have days like that myself!
Gerhard_Louis 3-Jan-15 8:46am    
I relooked at the way I was accessing the data and posted new code for the bntDelete eventhandler and it is working.Thanks again
OriginalGriff 3-Jan-15 10:09am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900