ok be gentle, I'm a new comer to WPF and LINQ - I've got a strange problem here. I've got a search screen as well as an add/edit screen. I've the add/edit screen certain to a 'CompanyContact' object along with a search screen certain to an assortment (CompanyContacts).

I've got a 3 column unique constraint (FirstName, LastName, CompanyId) around the CompanyContact db table which means you can't have a similar title appear two times for the similar company.

I ought to also point out that I've an "AFTER UPDATE" trigger on CompanyContact table's 'ModifiedDate' column which refreshes the ModifiedDate because I do not like permitting the customer PC to dictate the modifieddate/time... (I would like the database to keep an eye on once the record was modified). I allow the DEFAULT CONSTRAINT put GetDate() into this column on Card inserts.

let us say there's a "Steve Cruz" at CompanyId 123 and there's additionally a "Steve Smith2" at CompanyId 123

Basically make an effort to edit a current company contact (Steve Smith2 @CompanyId=123) and alter the surname from "Smith2" to "Cruz" to ensure that it causes the initial constraint to fireplace (collision with Steve Cruz @ CompanyId=123), everything appears to operate fine (i.e. I managed to get to ensure that the Edit screen traps the SqlException and 'resets' the qualities to their original values by resetting the bottom.DataContext and also the user is informed - "hey you cannot do this... it might result in a duplicate record") however when I dismiss the Edit screen (Click on the CANCEL button) and go back to the Search screen, the problem information is showing within the Search engine results... (i.e. you will find now 2 records showing Steve Cruz @ CompanyId 123)

I've attempted a lot of things, including writing code in LINQ to check on for replicates before trying to UPDATE... however it appears like there's a less complicated solution than that? I'm a large believer in putting rules in to the database so there's consistent enforcement of the rule i.e. to ensure that rules are enforced exactly the same for everybody including individuals individuals who make work directly from the database (around the after sales)

here is a snippet from Add/Edit screen (Search screen can refer to this as function)...

    public CompanyContact EditCompanyContact(int companyContactId)
    {
        CompanyContact myCompanyContact;

        try
        {
            _screenMode = ScreenMode.Edit;

            myCompanyContact = new CompanyContactRepository().GetById(companyContactId);

            //experimental code -- use this to reset base DataContext if unique constraint violated...
            _originalCompanyContact = (CompanyContact)myCompanyContact.Clone();

            //make sure to clone the object so we can discard changes if user cancels
            base.DataContext = (CompanyContact)myCompanyContact.Clone();

            SetupScreen();
            this.ShowDialog();

            //if user cancels Edit this is 'reset' to originalCompanyContact 
            return ((CompanyContact)base.DataContext); 

        }
        finally
        {
        }
    }

here is code in the 'cancel button'

    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            //HACK: this allows us to discard changes to the object passed in (when in EDIT mode)
            //TODO: research a truer WPF approach? (RoutedEvents?)
            _userCancelled = true;

            base.DataContext = _originalCompanyContact;

            this.Close();
        }
        finally
        {
        }
    }

this is actually the code that's performed whenever you try to reduce the Add/Edit screen:

try
{
  if (base.DataContext != null)
  {
   CompanyContactRepository ccr = new CompanyContactRepository();
   cc = ((CompanyContact)base.DataContext);
   ccr.Save(cc);
  }

  //dismiss the form after saving CompanyContact
  this.Close();
}
catch (SqlException sx)
{
  if (sx.Message.IndexOf("Violation of UNIQUE KEY constraint 'UN_CompanyContact_Value'.") == 0)
  {
    MessageBox.Show(String.Format("a CompanyContact with the name ({1} {0}) already exists for {2}", cc.FirstName, cc.LastName, cc.Company.Name), "Duplicate Record", MessageBoxButton.OK, MessageBoxImage.Exclamation);
  }
  else
  {
    //yes - catch and rethrow is probably a bad practice, but trying to ISOLATE UNIQUE constraint violation
    throw sx;
  }
}
finally
{
}

here is some LINQ code for that Save (sorry it's FUGLY! - I have been hacking around by using it all day long)

public void Save(CompanyContact entityToSave)
{
  try
  {

    var saveEntity = (from cc in db.CompanyContacts
            where cc.CompanyContactId == entityToSave.CompanyContactId
            select cc).SingleOrDefault();

    if (saveEntity == null)
    {
      //INSERT logic                    
      entityToSave.CreatedById = new CompanyPersonRepository().GetCompanyPerson(DataContext.Default.LoginUsername).CompanyPersonId;
      entityToSave.ModifiedById = entityToSave.CreatedById;

      db.CompanyContacts.InsertOnSubmit(entityToSave);
      db.CompanyContacts.Context.SubmitChanges();
    }
    else
    {
      //UPDATE logic            
      saveEntity.ModifiedById = new CompanyPersonRepository().GetCompanyPerson(DataContext.Default.LoginUsername).CompanyPersonId;
      saveEntity.CompanyId = entityToSave.Company.CompanyId;
      saveEntity.FirstName = entityToSave.FirstName;
      saveEntity.LastName = entityToSave.LastName;
      saveEntity.CompanyContactTypeId = entityToSave.CompanyContactTypeId;

      db.CompanyContacts.Context.SubmitChanges();

    }
...