0

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

hungariandude
  • 376
  • 2
  • 9
  • 23
  • Is CategoryID set as a Primary Key and set to auto increment in your database? Do you get an error? If so provide it please. You can place the db.SubmitChanges into a try catch if you're not getting an error... – jon.r Apr 28 '16 at 19:17
  • try dc.Categories.Add(c); dc.SaveChanges(); – techspider Apr 28 '16 at 19:18
  • Can you paste your Category class generated by EF; I supposed it doesn't carry Identity Information from database – techspider Apr 28 '16 at 19:21
  • Possible duplicate of [Entity Framework error: Cannot insert explicit value for identity column in table](http://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl) – techspider Apr 28 '16 at 19:23
  • I am not using Entity Framework, so .Add(c) couldn't be done. I'm not getting any error right now, but the new row doesn't show up. CategoryID is indeed a primary key and set to auto-increment by one. – hungariandude Apr 28 '16 at 19:28
  • When you supply the category ID, are you getting an error or does it just not insert? – stephen.vakil Apr 28 '16 at 19:49
  • I don't get any errors, it just simply doesn't save it to the table. – hungariandude Apr 28 '16 at 19:59

2 Answers2

1

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");
0

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

hungariandude
  • 376
  • 2
  • 9
  • 23
  • This cannot be problem of Visual Studio's version. I am afraid you are messing up with connection string for example and saved new rows in the different database then database where you checking results – Fabio Apr 29 '16 at 04:50
  • I'm using the same connection strings that VS generates for service based DBs. – hungariandude Apr 29 '16 at 15:56
  • You should elaborate deeper – oetoni May 16 '21 at 16:31