linq-to-sql

LINQ to SQL and Concurrency Issues

Hi, We are trying to build a High-Volume Orders Record System. There are three primary tables: 1. Orders 2. OrderDetails 3. OrderShipment The Shipment table contains n record per order and any record shipment entry can be changed before the Customer accepts th order, after which it is frozen. (A business requirement) Although this may...

Strange LINQ Exception (Index out of bounds)

I have a table, we'll call Users. This table has a single primary key defined in SQL Server - an autoincrement int ID. Sometimes, my LINQ queries against this table fail with an "Index was outside the range" error - even the most simplest of queries. The query itself doesn't use any indexers. For example: User = Users.Take(1); o...

Index of Linq Error

If I have the following Linq code: context.Table1s.InsertOnSubmit(t); context.Table1s.InsertOnSubmit(t2); context.Table1s.InsertOnSubmit(t3); context.SubmitChanges(); And I get a database error due to the 2nd insert, Linq throws an exception that there was an error. But, is there a way to find out that it was the 2nd insert that had...

LIKE in Linq to SQL

I have a method that needs to accept an array of country names, and return a list of records that match one of those country names. I'm trying this Public Shared Function GetConcessions(ByVal Countries As String()) As IEnumerable Dim CountryList As String = Utility.JoinArray(Countries) ' turns string array into comma-separated stri...

What's the Linq to SQL equivalent to TOP?

How do I do this Select top 10 Foo from MyTable in Linq to SQL? ...

Where to put extra functionality for the database layer (Linq-to-SQL)

What would be the best method to implement extra functionality in a database layer that uses Linq-to-SQL? Currently I'm looking at implementing functions for adding information based on presets and similar tasks? Inserts, updates and deletes requires access to the DataContext and in the Table classes you don't have access to the context...

Is LINQ to SQL InsertOnSubmit() subject to SQL Injection Attack?

I have code like this: var newMsg = new Msg { Var1 = var1, Var2 = var2 }; using (AppDataContext appDataContext = new AppDataContext(ConnectionString)) { appDataContext.CClass.InsertOnSubmit(newMsg); appDataContext.SubmitChanges(); } After reading this post I believe that the same logic applies. Does anyone think that...

LINQ to SQL - where does your DataContext live?

I'm using LINQ to SQL in a data access object library. The library is used in both web (web application/web service) and non-web (windows service) contexts. Initially, I stored the DataContext on the current HttpContext since it permitted me to manage a fairly small unit of work (one web request) and avoided global objects in a web app. ...

How can you see the sql that is causing an error on SubmitChanges in LINQ to SQL?

I've got some LINQ to SQL that sometimes throws a "Cannot insert duplicate key row in object 'dbo.Table' with unique index 'IX_Indexname'.The statement has been terminated." Is there some way I can turn on logging or at least debug into the datacontext to see what sql is being executed at the time that error is raised? Update: ...

LINQ to SQL associations - how to change the value of associated field

I have 2 classes with a LINQ association between them i.e.: Table1: Table2: ID ID Name Description ForiegnID The association here is between Table1.ID -> Table2.ForiegnID I need to be able to change the value of Table2.ForiegnID, however I can't and think it is because of the association (as wh...

Linq to SQL class lifespan

As far as I can understand, when I new up a Linq to SQL class, it is the equivalent of new'ing up a SqlConnection object. Suppose I have an object with two methods: Delete() and SubmitChanges(). Would it be wise of me to new up the Linq to SQL class in each of the methods, or would a private variable holding the Linq to SQL class - new'...

Linq to SQL - Can you submit changes for a single object?

Is there support in Linq to SQL for submitting changes to a single object? The SubmitChanges method sends all of the changes to the database, but what if I'm associating with an errorlog table and only want to save the records going into the errorlog without submitting all of the changes to my other tables? Example: Object Action (ID, ...

How can I implement Caching Strategy in my Asp.net Mvc With linq2sql repository?

I dont know if I should use the httpcontext caching or the Enterprise Library Caching Application Block. Also, what is the best pattern for the caching Strategy when deleting or updating an entity that is part of a cached list? Should I remove all of a list from the cache or only remove the item from the cached list? If I update it will...

How do I use SQL's GETDATE() and DATEADD() in a Linq to SQL expression?

If I have a Linq to SQL expression like this: from subscription in dbContext.Subscriptions where subscription.Expires > DateTime.Now select subscription I want this to to use the SQL Servers GETDATE() function instead of the time of the machine running the C# program. The next question would be how to translate DateTime.Now.AddDays(...

[LINQ] EntitySet vs Table

In a LINQ to SQL class, why are the properties that are created from the foreign keys EntitySet objects, which implement IEnumerable, where as the objects on the DataContext are Table objects which implement IQueryable? EDIT: To clarify, here is an example that illustrates what I'm trying to understand. This example: ctx.Matches.Where(...

How to get a calculated column back in Linq To SQL

We are using Linq To SQL with our own data context logic that executes the one linq query across multiple databases. When we get the results back, we need the database for each of the rows. So... I want to have a property on my class that will return the database name (SQL Server, so DB_NAME()). How can I do this in Linq To Sql? D...

Refreshing LinQ2SQL Model in Designer without losing all Properties

Hi, so I'm using LinQ2SQL quite heavily in my current application, and although I have most stuff in partial classes, some things have to be adjusted in the VS Designer (like Accessors for fields I wrap). Then, sometimes I like to name Fields differently in the Model than they do in the DB. So, my problem now is. When going back to cha...

Linq To SQL - how to have property not from table but from DB

We are using Linq To SQL with our own data context logic that executes the one linq query across multiple databases. When we get the results back, we need the database for each of the rows. So... I want to have a property on my class that will return the database name (SQL Server, so DB_NAME()). How can I do this in Linq To Sql? NOTE: ...

Linq to SQL: select optimization

On large tables in MSSQL; selecting specific columns results in greater speed of the query. Does the same apply to Linq to SQL? Would this: var person = from p in [DataContextObject].Persons where p.PersonsID == 1 select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode }; be faster than this: var person...

How do I ensure Linq to Sql doesn't override or violate non-nullable DB default values?

I have an SQL Server DB with a table with these fields: A bit with the default value 1, NOT NULL. A smalldatetime with the default value gettime(), NOT NULL. An int with no default value, IDENTITY, NOT NULL. When I generate Linq to SQL for this table, the following happens: The bit is given no special treatment. The smalldatetime i...