datacontext

How can I create generic datacontext with linq

How i can create a generic datacontext on linq means If database change i don't have to change anything on linq datacontext. Please specify the code or link ...

C# Linq-to-Sql - Should DataContext be disposed using IDisposable

I have several methods that deal with DB and all of them start by calling FaierDbDataContext db = new FaierDbDataContext(); Since the Linq2Sql DataContext object implements IDisposable, should this be used with "using"? using (FaierDbDataContext db = new FaierDbDataContext()) { // use db here } What are the implications of usin...

SqlMetal generating garbage association names

Why is SqlMetal messing up the Association names. For e.g. in my 'TextMessage' table i have two columns referencing the 'ApplicationUser' table. 'SenderUserId' and 'RecipientUserId' When I run SqlMetal and look at my 'ApplicationUser' class For 'RecipientUserId' it generates: [Association(Name="FK__TextMessa__Recip__72910220", Storage...

How can I tell if a given Linq-to-Sql object is attached to a datacontext?

I use linq 2 sql for my ORM. For performance reasons, I serialize some of them, and throw them into memcached. When they're deserialized, they're of course not attached to a datacontext, which is 100% fine, as they're only used for reading from in those scenarios. For sanity reasons however, I'd like to be able to tell whether a gi...

Silverlight: Determine whether DataContext is inherited or not

At runtime in a generic fashion (i.e. iterating UIElements) can I determine if a given FrameWorkElement has a non-inherited DataContext property set? I want a list of elements where DataContext was explicitly set, not inherited from higher up in the chain. I thought perhaps GetBindingExpression() would help but so far it has not. Usin...

Maximum 'Units of Work' in one page request ?

Its not One is it? I have a method that gets five Lists from different repositories. Each call opens and closes a new Datacontext. Is this ok to do or should I wrap everything in One datacontext. In this case it is not straightforward to use the same datacontext, but i am afraid that opening and closing numerous datacontext in one page ...

How to rollback the datacontext to the point of the last submitchanges

I am working on an app that is inserting a lot of new objects (rows) and relationships between them. But at a certain point when an error occurs I want all the changes to the DataContext be disgarded and "thrown away". So that after an error I have a clean copy of the DataContext that matches the state of the database. ...

ItemSource vs Datacontext in wpf.

Can any one give me the difference between Itemsource and DataContext of Listview in WPF? With example ...

How to use transactions with a datacontext

Can i use transactions with a datacontext, so i can rollback the state of the context after an error. And so yes, how does it work? ...

Testing Linqto SQL classes

How do I unit test my code that has LTS Datacontext. I get error while testing, I have a lot of Datacontexts and manually adding the Connection string is a pain, any suggestions. ...

Multiple DBML files - type sharing?

Hi, I have a Client/Server application, where the Client and Server have some common tables (which are kept in synchronisation as part of the application). We currently store these tables (i.e. FileDetails) in a Shared.dbml file. Until now, any stored proc that returns a result of set of FileDetails, has been placed in the Shared.dbml ...

Inject same DataContext instance across several types with Unity

Suppose I have IRepository interface and its implementation SqlRepository that takes as an argument LINQ to SQL DataContext. Suppose as well that I have IService interface and its implementation Services that takes three IRepository, IRepository and IRepository. Demo code is below: public interface IRepository<T> { } public class SqlRe...

Simulating Cross Context Joins--LINQ/C#

Here's the issue: I have 2 data contexts that I would like to do a join on. Now I know that LINQ doesn't allow joins from one context to another, and I know that 2 possible solutions would be to either create a single datacontext or to have 2 seperate queries (which is what I'm doing for now). However what I would like to do is to "sim...

WPF DataContext does not refresh the DataGrid using MVVM model

Project Overview I have a view which binds to a viewmodel containing 2 ObserverableCollection. The viewmodel constructor populates the first ObserverableCollection and the view datacontext is collected to bind to it through a public property called Sites. Later the 2ed ObserverableCollection is populated in the LoadOrders method and t...

How do I create a generic DataContext factory?

I'm building a generic Repository<T> class that supports Linq to SQL, and I'd like to use a factory pattern for the DataContext, because currently I have to pass the correct context to the constructor. Does anybody know how to determine the correct DataContext type for T, where T is a Linq to Sql Table? ...

What is the .cs file under MyDataContext.dbml for?

What is that individual (currently empty) file under MyDataContext.dbml for? Directory structure: Mydatacontext.dbml MyDataContext.cs MyDataContext.dbml.layout MyDataContext.designer.cs ...

C#: How to see if a Linq2SQL entity is in the database

I would like to check if an entity is already added to the database. So, how can I see this difference between a and b? var a = dataContext.Things.First(x => x.Name == something); var b = new Thing { Name = something }; To make it clearer, if I have this: var thing = dataContext.Things.FirstOrDefault(x => x.Name == something) ...

LinqToSql static DataContext in a web application

In a web application that I have run across, I found the following code to deal with the DataContext when dealing with LinqToSQL public partial class DbDataContext { public static DbDataContext DB { get { if (HttpContext.Current.Items["DB"] == null) HttpContext.Current.Items["DB"] = new DbDataCont...

Deleted Rows Remain in my Data Context Unless I Initialize It Again

I have a datacontext say myDataContext, it has on several classes (tables) and stored procedures, one of which deletes directly from the database (the logic requires this), however, the deleted rows remain in my myDataContext unless I initialize it again. Is there a way to avoid this? The stored procedure removes the record from the data...

DbLinq - Cache problem

I'm using linq to sql for MySql (using DbLinq) in an ASP.NET MVC website. I have a weird caching problem. Consider the following methods in my Repository class: public IEnumerable<Message> GetInbox(int userId) { using(MyDataContext repo = new MyDataContext(new MySqlConnection("[Connectionstring]"))) { return repo.Messages.Where(...