LINQ InsertOnSubmit Required Fields needed for debugging

Posted by Derek Hunziker on Stack Overflow See other posts from Stack Overflow or by Derek Hunziker
Published on 2010-04-13T16:36:21Z Indexed on 2010/04/13 17:43 UTC
Read the original article Hit count: 423

Hi All,

I've been using the ADO.NET Strogly-Typed DataSet model for about 2 years now for handling CRUD and stored procedure executions. This past year I built my first MVC app and I really enjoyed the ease and flexibility of LINQ. Perhaps the biggest selling point for me was that with LINQ I didn't have to create "Insert" stored procedures that would return the SCOPE_IDENTITY anymore (The auto-generated insert statements in the DataSet model were not capable of this without modification).

Currently, I'm using LINQ with ASP.NET 3.5 WebForms. My inserts are looking like this:

ProductsDataContext dc = new ProductsDataContext();
product p = new product 
{
        Title = "New Product",
        Price = 59.99,
        Archived = false
};

dc.products.InsertOnSubmit(p);
dc.SubmitChanges();

int productId = p.Id;

So, this product example is pretty basic, right, and in the future, I'll probably be adding more fields to the database such as "InStock", "Quantity", etc... The way I understand it, I will need to add those fields to the database table and then delete and re-add the tables to the LINQ to SQL Class design view in order to refresh the DataContext. Does that sound right?

The problem is that any new fields that are non-null are NOT caught by the ASP.NET build processes. For example, if I added a non-null field of "Quantity" to the database, the code above would still build. In the DataSet model, the stored procedure method would accept a certain amount of parameters and would warn me that my Insert would fail if I didn't include a quantity value. The same goes for LINQ stored procedure methods, however, to my knowledge, LINQ doesn't offer a way to auto generate the insert statements and that means I'm back to where I started.

The bottom line is if I used insert statements like the one above and I add a non-null field to my database, it would break my app in about 10-20 places and there would be no way for me to detect it. Is my only option to do a solution-side search for the keyword "products.InsertOnSubmit" and make sure the new field is getting assigned?

Is there a better way?

Thanks!

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about stored-procedures