datacontext

Will transactionscope work over multiple calls to different services?

I'm writing some merge functionality in C# asp.NET MVC2. I am also using using Linq2SQL. I have a block of code which calls two services, MessageService and UserService. These both in term call their appropriate repositories and make the amendments to the db. Each repository declares it's own instance of the repository so I'm thinking t...

Linq-to-sql datacontext class design question?

I'm using linq-to-sql in a web scenario only. I know it is recommended to always wrap the datacontext in a using but in a web scenario only, the kind of scenario I'm designing for, this is really not necessary because each page will spawn and die in very short time. Given that background, I've looked at extended some of my linq-to-sql c...

Defining DataContext from controller code

Hello, I wish to define the DataContext of my window from an external class to be able to use DI for my data model. I have read some tutorials about it but I still can't get it to work together. Say we have a simple data model : class Data { public String Value { get; set; } public Data() { Value = "Test"; } ...

Dynamic data website with 1 datacontext but multiple connection strings

Is there any way to change the connection string at runtime when working with the Dynamic Data framework and LINQ. There seems to be no way to register multiple Models (with a single shared DataContext) at runtime, I just get an exception saying the specified DataContext type has already been registered. I have tried using multiple mod...

SQL Server timeout exception when extending Linq partial methods

In .NET 4.0 and Linq to SQL, I am trying to use a partial class to "trigger" changes from within an update method (an existing DBML method). For simplicity, imagine a table Things with columns Id and Value The auto gen DBML contains a method OnValueChanged, I'll extend that and as an exercise try to change one value in one other row : ...

LINQ DataContext inserts object that is not attached

I have created two separate objects, one of class Order and one of class TempOrder. When I try to insert the TempOrder object in the db using db.TempOrders.InsertOnSubmit(obj) and then calling db.SubmitChanges the Order object gets submitted as well (without being attached to the datacontext or anything). There is no relationship betwe...

LINQ-to-SQL performance issue for mass inserts.

Hi, I have identified a problem within my application; basically, one sub-routine prepares (lots) of data that is later on inserted into my local database via a LINQ-to-SQL data context. However, even a relatively modest amount of new data (100,000-ish) takes a tremendous amount of time to be saved into the database when SubmitChanges()...

Difference between ItemsSource and DataContext as pertains to ListBox

I am not quite grokking the difference between ItemsSource and DataContext. Can someone explain it and back it up with examples? When would I use one or the other. I am reading the docs and it says that I can bind using DataContext, but I throw an ObservableCollection at it and nothing shows up in the list. If I throw the same collec...

WPF + MVVM - How to bind a property to the parent view's data context.

Working with the MVVM pattern, I have a pair of view model classes that represent a two-tier data hierarchy, each with a corresponding UserControl that represents its view. Both view model classes implement INotifyPropertyChanged and the root level view model exposes a property that is relevant to both its own view and the child view. T...

Reverting changes to an EntitySet<T> in Linq-to-SQL

I have a Linq-to-SQL context with changes in an EntitySet. I want to get rid of all the changes I made to it since I last called SubmitChanges(). I use the following method to revert the state of the context: public static void RevertChanges(this DataContext context) { var changeSet = context.GetChangeSet(); var inserts = change...

Why Microsoft add Design-time Attributes (d:DataContext ...) and do not use the current DataContext instead

In WPF-XAML, determining DataContext at designtime vs runtime. Why do we have to add "d:DataContext" while we already have set the "DataContext" ? Why Microsoft introduced DesignTime attributes (code that we have to add, a little bit complex) when it could already be determined automatically by itself using "DataContext" attribute. The ...

Access DataContext behind IQueryable

Is it possible to access the DataContext object behind an IQueryable? If so, how? ...

LINQ: Call Stored Procedure and Join its results with IQueryable

I have: a stored procedure which retrieves zip codes in a radius around another zip code a table with contacts I'm getting the contacts through an IQueryable interface which I've extended with various extension methods to filter results by age etc. What I have trouble with is adding an IQueryable extension method which calls the st...

How to create Listbox dynamically wpf

Hi All, I want to create listbox dynamically [codebehind c#]. Its datasource is class object. class sample { string filepath; string id; string trackName; } Needs: Display trackName as listbox item in the listbox [dynamically]. Code: sample samp=GetBL.GetValue(); ListBox lbTrack = new ListBox(); StackPanel sp = new StackPa...

Why would reusing a DataContext have a negative performance impact?

After a fair amount of research and some errors, I modified my code so that it creates a new DataContext each time the database is queried or data is inserted. And the database is queried frequently - for each of 250k transactions that are processed, the database is queried to obtain a customer id, department id, and category before the...

Can I reuse the connection from DataContext in Linq to Sql?

DataContext has Connection property of type DbConnection and I was wondering if I could re-use it. I've tried to use it creating a command using CreateCommand and using a reader off of it but I've gotten errors saying there is a pending transaction or something similar. Basically I'm trying to find out if there is a best practice or gu...

How to prevent Silverlight RIA Entity getting attached to datacontext before i'm ready

I have a Silverlight 4 application for a simple 'TODO' list. The problem I'm having is that databinding is hooking up relationships on my TODO object, which causes the RIA data context to add it to the DataContext.TODOs list before I want it there. I want to treat the object as new and detached until I'm explicitly ready to add it to th...

Trouble putting Statement Lambda inside a LINQ query

I'm trying to inject some inline work as a Statement Lambda into a LINQ query select like so... // NOTE: mcontext.Gettype() == System.Data.Linq.DataContext // Okay - compiles, nothing unusual var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples select person; // ERROR - see below compile Error - Can I retrofit ...

Generate LINQ DataContext from XML, XSD, or C# class

Hi, I have a massive set of classes I need to create from an XML file that I'd really not like to do by hand. What I'd really like to do is somehow generate a LINQ DataContext so I can use VisualStudio to build a SQL database from the DataContext. I'm aware that I can generate XSD and C# classes from the XML file, but are there any tool...

Random DataReader errors and thread-specific DataContext allowing for changing DataLoadOptions.

My .NET MVC project has reached the stage of testing with multiple users and I am getting seemingly random errors (from any screen in the site) relating to Linq2Sql DataReader issues, such as: 'Invalid attempt to call FieldCount when reader is closed.' and 'ExecuteReader requires an open and available Connection. The connection's current...