linq-to-sql

How to assign null to sql entity columns

I have generated linq to sql entites but cannot figure out how to assign null to a nullable column. whenever i try to assign null to it it says "there is no implicit type conversion between int and ". BTW the type of the field is int? and the database column is also nullable. ...

Can LINQ to SQL generated objects be decoupled?

I like LINQ to SQL, but it seems like the classes it generates are tightly coupled to the database they are stored in, which seems like a Bad Thing. For example, using ye olde Northwind database, if I create the dbml with the Products table, a Product class is generated. I can use this class in any other tier, which is all well and goo...

Extend a LINQ entity-class with constructor methods and make that entity-class inherit from it's DataContext class

Is it possible to extend LINQ-to-SQL entity-classes with constructor-methods and in the same go; make that entity-class inherit from it's data-context class?--In essence converting the entity-class into a business object. This is the pattern I am currently using: namespace Xxx { public class User : Xxx.DataContext { pub...

ASP.NET MVC: Best way to get form checkboxes into many-to-many DB assoc table with LINQ To SQL?

Hi, I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach (Category c in (List<Category>)ViewData["cats"]) { if (selCats.ContainsKey(c.ID)) { %> <input name="CategoryIDs" type="checkbox" value="<%=c.ID %>" checked="checked" />&nbsp;<%= c.Name%><% } else { %> <inpu...

LINQ to SQL entity and data-context classes: business object encapsulation

What are your favorite ways to encapsulate LINQ to SQL entity classes and data-context classes into business objects? What have you found to work in a given situation? Have you invented or taken to any specific patterns? ...

Update columns in one LINQ object with another LINQ object.

Hi all, I have two LINQ objects which have exactly the same columns and I would like to be able to update one with the fields from the other. I first create a new object from some data in a file, then I query the database for an existing item with the same ID. What I would like to be able to do is update the existing objects details w...

Cleanest syntax to delete a record in linqtosql

What is the best way to delete a database record using LINQ when I have the primary key? ...

LINQ to SQL with RDBMS other than MS SQL Server

Is there any plans for Microsoft to support LINQ to SQL beyond MS SQL server? ...

What is the best way to sort using a GridView and LINQ?

The one thing that LINQ seems to be missing for me is a way to reference columns by text string. For example, I have a typical GridView set up with sorting like this (the DataSource is bound to a LINQ query in the code-behind): <asp:GridView ID="MyGridView" runat="server" AllowSorting="True"> <Columns> <asp:BoundField DataFi...

Multiple/single instance of Linq to SQL DataContext

I have a project with a number of different classes querying and modifying data in a common set of tables. I've set up a .dbml file which provides us with a DataContext class. My question is whether a single instance of the DataContext should be used by all objects, or whether multiple instances are safe to use. I'm also wondering about ...

LINQ to SQL concurrency conflict - Looks like a clean attach with proper row versioning

I am trying to get LINQ to SQL to persist changes to an attached object wherein the backing table has a DateTime column that I think should function for row versioning, as described here. The table looks like this: CREATE TABLE [dbo].[client]( [client_id] [int] IDENTITY(1,1) NOT NULL, [client_address1] varchar(100) NULL, /* snip */ [mo...

What's the best way to copy and insert data from related tables into a new set of related tables with Linq

I work on a web game , I use asp.net mvc, linqtosql. I have 3 tables that store default game values: DefaultRegions, DefaultSubRegions and DefaultCountries. These 3 tables are related to each other thanks to primary / foreign keys. I need to copy the values of these tables into 3 tables called Regions, SubRegions and Countries when som...

How do you skip default value columns on insert/update in Linq To SQL?

How do you insert/update a column through Linq To SQL and Linq To SQL use the default values? In particular I'm concerned with a timestamp field. I've tried setting that column to readonly and autogenerated, so it stopped trying to put in DateTime.MinValue, but it doesn't seem to be updating on updates. ...

Entity classes decoupled from LINQ to SQL provider for implementing the Repository pattern. How?

I have looked over the Repository pattern and I recognized some ideas that I was using in the past which made me feel well. However now I would like to write an application that would use this pattern BUT I WOULD LIKE TO HAVE THE ENTITY CLASSES DECOUPLED from the repository provider. I would create several assemblies : an "Interfaces...

LINQ to SQL multiple DataContext-s

I'm trying to figure the best strategy about how to organize DataContexts. The typical DB we work has between 50 and 100 tables usually in third-normal form and with many relations between them. I think we have two options: Put all tables in a single context. This will ensure that anything we do will be committed in the correct order i...

StructureMap and SqlCacheDependency

I'm trying to enable SqlCacheDependency through my StructureMap IoC, I'm using LinqToSql I have the code done to take care of the Linq Caching but not quite sure how to go about setting up the SqlCacheDependency as it requires putting this in a global.asa file void Application_Start(object sender, EventArgs e) { string connectionSt...

Dynamic where clause in LINQ - with column names available at runtime

Disclaimer: I've solved the problem using Expressions from System.Linq.Expressions, but I'm still looking for a better/easier way. Consider the following situation : var query = from c in db.Customers where (c.ContactFirstName.Contains("BlackListed") || c.ContactLastName.Contains("BlackListed") || c.Add...

Linq DataContext class not accessible in my MVC after I save the dbml

I just downloaded MVC and I am going through a tutorial. Everything goes fine until I try to declare a DataContext object. My dbml is named db.dbml (tried another on named test.dbml) and when I try this: public dbDataContext db = new dbDataContext(); I get: The type or namespace name 'dbDataContext' could not be found ... Am...

Connection string hell in .NET / LINQ-SQL / ASP.NET

I have a web application that comprises the following: A web project (with a web.config file containing a connection string - but no data access code in the web project) A data access project that uses LINQ-SQL classes to provide entities to the web project UI (this project has a settings file and an app.config - both of which have con...

Linq to Sql - Hierarchical Query to Find Ancestors

Given an EmployeeId, how can I construct a Linq to Sql query to find all of the ancestors of the employee? Each EmployeeId has an associated SupervisorId (see below). Entity Definition: Sample Table Data: For example, a query of the ancestors for EmployeeId 6 (Frank Black) should return Jane Doe, Bob Smith, Joe Bloggs, and Head H...