views:

59

answers:

2

I am having an issue adding an item to my dataset in Linq to SQL. I am using the exact same method in other tables with no problem. I suspect I know the problem but cannot find an answer (I also suspect all i really need is the right search term for Google). Please keep in mind this is a learning project (Although it is in use in a business)

I have posted my code and datacontext below. What I am doing is:

Create a view model (Relevant bits are shown) and a simple wpf window that allows editing of 3 properties that are bound to the category object. Category is from the datacontext. Edit works fine but add does not. If I check GetChangeSet() just before the db.submitChanges() call there are no adds, edits or deletes.

I suspect an issue with the fact that a Category added without a Subcategory would be an orphan but I cannot seem to find the solution.

Command code to open window:

CategoryViewModel vm = new CategoryViewModel();
AddEditCategoryWindow window = new AddEditCategoryWindow(vm);
window.ShowDialog();

ViewModel relevant stuff:

public class CategoryViewModel : ViewModelBase
{
    public Category category { get; set; }

    // Constructor used to Edit a Category
    public CategoryViewModel(Int16 categoryID)
    {
        db = new OITaskManagerDataContext();
        category = QueryCategory(categoryID);
    }

    // Constructor used to Add a Category
    public CategoryViewModel()
    {
        db = new OITaskManagerDataContext();
        category = new Category();
    }
}

The code for saving changes:

// Don't close window unless all controls are validated
if (!vm.IsValid(this)) return;
var changes = vm.db.GetChangeSet(); // DEBUG
try
{
    vm.db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
    vm.db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
    vm.db.SubmitChanges();
}

The Xaml (Edited fror brevity):

<TextBox Text="{Binding category.CatName, 
                Mode=TwoWay, 
                ValidatesOnDataErrors=True, 
                UpdateSourceTrigger=PropertyChanged}" />
 <TextBox Text="{Binding category.CatDescription, 
                ValidatesOnDataErrors=True, 
                UpdateSourceTrigger=PropertyChanged}" />
 <CheckBox IsChecked="{Binding category.CatIsInactive, Mode=TwoWay}" />

Data Context

  • IssCategory in the Issues table is the old, text based category. This field is no longer used and will be removed from the database as soon as this is working and pushed live.

Update and Answer

JayD had the answer below with the need to add:

db.Categories.InsertOnSubmit(category); 

I was curious why I needed this here, but had never needed it before, to add new items to the database. It turns out that db.SubmitChanges() only submits Changes to existing entities and their dependencies. The reason why this was working for me before was that in all prior cases my new entities had dependencies already persisted in the database. In this case a new Category is an orphan until a Subcategory is added later.

+1  A: 

I think your suspicion is correct. If you are not adding a new SubCategory when adding a new Category I would suggest you remove that association in your Dbml. I may have misunderstood you question but based on my experience this is very similar to an issue i had. Also can you elaborate on the error you are seeing?

JayD
I do not see any error at all. My thought initially was that I needed to get a Category in before adding a subcategory that is associated with it. Maybe I was thinking wrong, since all Subcategories can only belong to one Category it made sense to me to make the association on the subcategory level then use the Subcategory foreign key on the issues table leaving hte categories off by itself.
Mike B
+1  A: 

Are you doing the

db.Categories.InsertOnSubmit(category);

???

Traci