linq-to-sql

How to customise properties on a Linq2SQL entity?

I know you can customise entities through partial classes, but what is the general approach for modifying properties on entities? What if i want to change the logic in the get/set? What about adding attributes? I don't want to edit the auto generated code - how do people get around this? ...

How can I mimick nhibernate's repository pattern with linq2sql?

I was reading this blog entry: http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx I like how they made a interface that has all the basic CRUD queries for you, and you can use it accross all your entities/tables. Can this also be done with linqtosql as well? Code: public class Reposit...

Need help converting query with joins and group by/count to LINQ to SQL

I need help converting this query to linq to sql: select rd.RouteDispatchID, r.RouteNumber, s.ShortDescription Status, rd.DispatchDate, rd.CreationDate CreatedDate, e.FirstName, e.LastName, count(md.MachineDispatchID) NumMachines from dbo.RouteDispatch rd inner join dbo.Route r on rd.RouteID = r.Route...

Linq to Sql: Am I doing this right?

Hey everyone, I am working on a database access layer project and decided to use Linq to SQL for it. One of the things I wanted to do was provide an API that can take Linq Expressions as arguments to retrieve data. For instance, I wrote this API now that looks like this: public Result GetResults(System.Linq.Expressions.Expression<Func...

LinqToSql - mapping exception when using abstract base classes

Problem: I would like to share code between multiple assemblies. This shared code will need to work with LinqToSql-mapped classes. I've encountered the same issue found here, but I've also found a work-around that I find troubling (I'm not going so far as to say "bug"). All the following code can be downloaded in this solution. Given...

Many-to-many insert using LINQ to SQL and ASP.Net MVC

I have two Tables: Products(ProductID, ProductName) and Categories(CategoryID, CategoryName) with a many-to-many relationship between Products and Categories ProductsCategories(ProductID, CategoryID) How Can I Implement a view that enables a user to Add and Edit one product with many categories using Asp.Net Mvc and LINQ to SQL? ...

I need some clarification on the MVC architecture and the three-tier architecture...

Hi, I've been reading the book Pro ASP NET MVC Framework and I'm getting really confused with a lot of things. I've been trying to do some research but I'm finding that with so many different approaches and concepts being thrown at me, it's just making things worse.So I have a few questions: I know MVC is supposed to split the functio...

linq to sql + stackoverflow exception when querying objects

OK, i have confirmed i only this issue when i attempt to query on the primary key if that primary key in the entity is set to 'Auto Generated Value' -- but w/o this, how can i insert? Sorry if this is a noob linq2sql but i just started working with it. How does one go about using Linq to Sql with this option turned off but also have th...

linq to sql + update table

Heres a newb question for you. I have a multi tier environment so i dont have the origional datacontext where the item was created, thus i am having an issue getting the table to update correctly - here is what im doing: 1.) Get the object from the DAL layer 2.) make changes 3.) call update on the DAL layer and pass the modified ent...

How do I convince Linq to Sql to generate Sql to compare strings with greater than or less than?

Let's say I have a MS-SQL 2005 table named "People" with the following rows: |FirstName|LastName| |JD |Conley | |Joe |Schmo | |Mary |Jane | I want to execute a SQL statement like: select * from People where FirstName > 'JD' The problem I'm having is I can't think of a way to get LINQ to SQL to generate this SQL...

Invalid Cast Exception, Stored Procedure, LINQ TO SQL

9 months later, the same problem shows up again. I've tried everything I can think of: I cast as varchar(max) on the stored procedure; I changed the mapping; tried to find the collection linq works with, but couldn't find anywhere; I have been running this stored procedure for some time Mapping it to my objects using LINQ to SQL, and...

Linq to SQL MVC issue with FK

right having a bit of a hard mental blog with some Linq to SQL and MVC. Getting the basic MVC and L2SQL going ok. FK relationships. -> when using MVC and we have a fk relationship in the model, and the view is based on a strongly typed object - how do you get the data from the related table? So for example User (Table) UserId Use...

Does anyone use the generated entity classes on a large project?

In the NerdDinner example they use a repository pattern to decouple the business from the data layer. But then they use the Linq to SQL generated classes (Dinner specifically) as the entity class used throughout the project. So how decoupled is that really? It’s not like you could easily exchange Linq-to-SQL. On my last project I creat...

LinqToSQL - Cast IQueryable object at runtime?

Given this: var query = context.GetTable<T>(); Where "T" is a generic entity passed into the method, I'd like to do something like this: if(typeof(TEntity) is IEntitySoftDeletable) query = query.Cast<IEntitySoftDeletable>.Where(ent => !ent.IsDeleted); } Is this possible? Currently it's telling me that I can't cast this way. I ...

LINQ to SQL inheritance question

Hi, I am doing the below test to try and learn more about LINQ to SQL. I have got an Activities table which contains an activityId, parentId, type. The type is used as a discriminator value, to say whether it is an activity, task or project. I have an Activity, Task and Project class that inherit from Activity, it seems wrong for Tas...

Is there a way to check if a Linq to SQL entity has been modified?

The data context can do it, but I need to circumvent the SubmitChanges function for a bit due to having a two stage insert process and not enough time to figure out how to make it work the right way. There are a few things which are lists of items that may or may not be modified, and I'd like to only submit the actually modified items t...

IF statement inside a LINQ SELECT

I'm trying to produce an IF conditional statement inside of a "select new" that checks the values of two fields in order to fill a property. from e in Employees where e.EmployeeID == id select new { EmployeeID = e.EmployeeID, EmployeeName = e.FirstName + " " + e.LastName, Status = (if e.col1.HasValue then "This Value" else i...

How can I write a clean Repository without exposing IQueryable to the rest of my application?

So, I've read all the Q&A's here on SO regarding the subject of whether or not to expose IQueryable to the rest of your project or not (see here, and here), and I've ultimately decided that I don't want to expose IQueryable to anything but my Model. Because IQueryable is tied to certain persistence implementations I don't like the idea ...

LINQ to SQL not inserting child objects correctly

If have an object hierarchy like this: A - B - C where each A can have multiple Bs and each B multiple Cs. I can use LINQ to SQL to insert my A using dataContext.As.InsertOnSubmit(myA) and it correctly inserts all the child objects as well. So far so good. The problem is when I want to update an A that I have already saved by a...

LINQ to SQL Basic insert throws: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.

I am trying to insert a record. This code worked but has stopped working I don't know why. Here is the code: using (SAASDataContext dc = new SAASDataContext()) { tblAssessment a2 = new tblAssessment(); a2.AssessmentCentreId = centreId; a2.Attemp...