hungariandude
hungariandude

Reputation: 386

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"
};
dc.Categories.InsertOnSubmit(c);
dc.SubmitChanges();

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).

Upvotes: 0

Views: 389

Answers (2)

user2820310
user2820310

Reputation: 11

I think I found a solution that works in Visual Studio 2015. Go to your Server Explorer and open the properties of the .mdf. Copy the Connection string and paste it into the closing parenthesis and put it inside a verbatim string literal(@" ") and remove all quotes inside(if there are any).

DataClasses1DataContext dc = new DataClasses1DataContext(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\FilePathHere\Database.mdf;Integrated Security=True");

Upvotes: 1

hungariandude
hungariandude

Reputation: 386

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

Upvotes: 0

Related Questions