linq-to-sql

LINQ to SQL: To Attach or Not To Attach

So I'm have a really hard time figuring out when I should be attaching to an object and when I shouldn't be attaching to an object. First thing's first, here is a small diagram of my (very simplified) object model. In my DAL I create a new DataContext every time I do a data-related operation. Say, for instance, I want to save a new us...

Mock a Linq to Sql EntityRef using Moq?

My datacontext contains a table named Userlog with a one to many relationship with table UserProfile. In class UserLog public UserProfile UserProfile { get {return this._UserProfile.Entity;} } In class UserProfile public EntitySet<UserLog> UserLogs { get{return this._UserLogs;} } { set {this._UserLogs.Assign(value); } How wo...

.DBML file and LINQ to SQL

In my DBML file I have mapped some tables and stored procedures, and the stored procedures return type is ISingleResult . T is some mapped table. But I want to take the data into my own created entities rather than LINQ to SQL created entites. The entites created by me are also the same as the mapped table entities and their use lies whe...

[C#] How to create a constructor of a class that return a collection of instances of that class?

My program has the following class definition: public sealed class Subscriber { private subscription; public Subscriber(int id) { using (DataContext dc = new DataContext()) { this.subscription = dc._GetSubscription(id).SingleOrDefault(); } } } ,where _GetS...

Repository Pattern: SaveOrUpdate() in Entity Framework and L2S

These web articles uses separate Save() and Update() methods for saving and updating an entity. How can I write a SaveOrUpdate() method in Entity Framework with the help of ObjectContext and in Linq-To-SQL with the help of DataContext? That is, how can I write a single method that shall do both save and update job? ...

LINQ TO SQL, ADO.NET Entity Framework, T-SQL

Greetings, I have a few applications/websites running with LINQ to SQL and the other day I decided to go ahead and optimize some of the queries, etc and I found that the size for variable length data types is derived from the parameter value instead of the column actual size? for example a column is defined as nvarchar(30). when I use ...

What is the difference between these two LINQ statements?

I had the 1nd statement in my code and found it not giving an accurate count, it was returning 1 when the correct answer is 18. To try and debug the problem I broke it out creating the 2nd statement here and the count returns 18. I just don't see what the difference is between these two. It seems like the 1st is just more compact. I'm c...

Is a notification registered in SQL Server for a failed SqlCacheDependency's SqlCommand ExecuteScalar?

We have some code that hits our SQL Server 2005 database via LINQ to SQL to read a list of objects from a table, converts them to our serializable middle tier version of the objects, then caches that list using an SqlCacheDependency. However, at several times throughout the day, a large update on the underlying table occurs which seems...

How to use LINQ for CRUD with a simple SQL table?

Every LINQ blog I found there seemed around 2 years old, I understand the syntax but need more direction on creating the SQL mapping and context classes. I just need to use LINQ for 2 SQL tables I have, nothing complicated. Do folks write the SQL mapping classes by hand for such cases or is there a decent tool for this? Can someone po...

Inheriting LINQ-to-SQL data context from base controller

My base controller class, BaseController, is inherited by public-facing controllers to access a shared data context between requests with LINQ-to-SQL. Am I accessing my data context in an efficient and safe way by storing it in HttpContext.Current.Items for each HTTP request? DataContextHelper class internal static class DataContext...

Help me eliminate redundant code

I have an ASP.NET (3.5) page that allows a user to upload an Excel 2003 file with multiple sheets and that data is inserted into staging tables in the database. The mapping of database tables/columns to Excel sheets/columns is specified in an XML file. I use LINQ to SQL to send the data to the database. ImportTablesDataContext db = ne...

why LINQ 2 SQL sometime add a field like select 1 as test, others valid fields....

I have to concat 2 linq2sql query and I have an issue since the 2 query doesn't return the same number of columns, what is weird is after a .ToList() on the queries, they can concat without problem. The reason is, for some linq2sql reason, I have 2 more column named test and test2 which come from 2 left outer join that linq2sql automati...

How can I bind an Enum to a DbType of bit or int?

Hi I am using Linq2Sql and want to bind an objects field (which is enum) to either a bit or a int type in the database. For example I want have a gender field in my model. I have already edited the DBML and changed the Type to point to my enum. I want to create Radio buttons (which I think I have figured out) for gender and dropdown lis...

Why are my connections not closed even if I explicitly dispose of the DataContext?

I encapsulate my linq to sql calls in a repository class which is instantiated in the constructor of my overloaded controller. The constructor of my repository class creates the data context so that for the life of the page load, only one data context is used. In my destructor of the repository class I explicitly call the dispose of the...

LINQ Entity Serialization with Memcached

I'm working on integrating Memcached (using the Enyim Memcached library for .NET) in my application and would like to cache data at every layer (data, business, app). I'm using LINQ to SQL for the data access layer. In trying to cache LINQ entites I ran into a problem in that Enyim uses the Binary formatter for serialization, which is ...

Linq The specified type 'string' is not a valid provider type.

Using Linq to call a stored procedure that passes a single string, The stored procedure returns a data set row that contains a string and an int. Code: PESQLDataContext pe = new PESQLDataContext(strConnStr); pe.ObjectTrackingEnabled = false; gvUnitsPassed.DataSource = pe.PassedInspection(Line); gvUnitsPassed.DataBind(); p...

Index was outside the bounds of the array. IndexOutOfRangeException in LINQ to SQL

Im getting this exception in the protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); <---- HERE !!! } } of one recently table association i created, there lots of same...

LINQ to SQL for tables across databases. Or View?

I have a Message table and a User table. Both are in separate databases. There is a userID in the Message table that is used to join to the User table to find things like userName. How can I create this in LINQ to SQL? I can't seem to do a cross database join. Should I create a View in the database and use that instead? Will that work?...

Weird mapping error in linq-to-sql dbml file in VS2010

Since I switched to VS2010, several times a day I get a compilation error in my dbml file: DBML1005: Mapping between DbType 'bigint' and Type 'MyNamespace.SecurityToken' in Column 'SecurityToken' of Type 'Employee' is not supported When I restart VS2010 the error disappears. I have no problems running my application using this dbml fi...

How to use predicate builder with linq2sql and OR operator

I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many relationship respectively and a foreign key between ID columns of both tables. Using linq2sql I am trying to select all TABLE1 entries such that their corresponding TABLE2 values contains at least 1 item in the list I pass it. Here's some sample code I was using in...