hungariandude hungariandude - 1 year ago 134
C# Question

Service based database & LINQ To SQL, inserting new row

I have a really basic table called Category in a service based database, looking like this:

CategoryID: Primary key, identity

CategoryName: Just an nvarchar(max) column

I have added a LINQ To SQL class to the project, and initialized it right before my form's constructor:

DataClasses1DataContext dc = new DataClasses1DataContext();

And now I just want to do something simple: add a new row to the Category table.
I have tried this:

var c = new Category()
CategoryName = "test"

I have tried it both with CategoryID = 4, and without it (4 would be the next available ID).

I have also set the database's copy to output directory property to copy if newer.

I even tried removing the identity from CategoryID (though that shouldn't be a good solution), but then I got an exception that CategoryID shouldn't be null (even when I set it to 4).

(I am not using Entity Framework).

Answer Source

Found the problem (kinda). The code works perfectly well with Visual Studio 2013, therefore I'm fairly sure the problem is with VS2015.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download