linq-to-sql

LINQ Select Query Question

I have the following entities: CartoonStory CartoonFigure Sex A CartoonStory is told by one or more CartoonFigures and a CartoonFigure is of a Sex male or female. When the user of my website is for example a female I only want to retrieve CartoonFigures with the Sex female if there are any. If this is not the case retrieve the Male ...

why is the second DatabaseConflictException being thrown?

I have some linq2sql stuff which updates some rows. Then when I submit I do this: try { database.SubmitChanges(); } catch (ChangeConflictException) { database.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges); database.SubmitChanges(); } Now the second submit(the one in the catch) is throwing yet again a ChangeConflictEx...

Attribute.IsDefined doesn't see attributes applied with MetadataType class

If I apply attributes to a partial class via the MetadataType attribute, those attributes are not found via Attribute.IsDefined(). Anyone know why, or what I'm doing wrong? Below is a test project I created for this, but I'm really trying to apply custom attributes to a LINQ to SQL entity class - like this answer in this question. Tha...

Tracking external changes to a database with LINQ-to-SQL

Is there a way to get SQL Server 2005 to call back to a connected application, such that the connected application will know when a record in a table has had a field modified by another application using the same database? A simple example would be two instances of the same application connecting to the same table in the same database. ...

DataContext across multiple databases

I have an application that needs to join tables from multiple databases into a single LINQ-to-SQL query. Unfortunately, I have a separate DataContext class setup for each database, so this query won't work. I get an error like this: "The query contains references to items defined on a different data context" The ideal solution seems ...

WPF and LINQ/SQL - how and where to keep track of changes?

I have a WPF application built using the MVVM pattern: My Models come from LINQ to SQL. I use the Repository Pattern to abstract away the DataContext. My ViewModels have a reference to a Model. Setting a property on the ViewModel causes that value to be written through to the Model. As you can see, my data is stored in my Model, and ...

DataContext.GetTable(Type) question

I just started using LINQ a couple of days ago and i'm stuck now. Given a certain type i need to recurse to the top. For example: hierarchy: Client, Project, Department. proposed function descriptor to load hierarchy: public static IEnumerable<object> LoadPath<T>(int id) where T : class, ContentItem, new() { } the user specifi...

Unit testing Linq 2 sql with dbnull

I am writing a unit test for a method which fills an object from a datatable. In one specific unit test, I want to check how the object will be filled if the datarow has a null value in it. If I assign a DBNull.value to the datarow, I get an exception while running the test. I have posted the Subject under test below - return dt.AsEnu...

Live, shared hosted, ASP.NET MVC site migration to Windows Azure

I have an existing ASP.NET MVC based website. Very typical: XHTML, CSS, jQuery, C#, LINQ2Sql. Web.config tells app where to connect for SQL database. Are typical websites like this easy to port to Windows Azure? What sort of headaches should I be ready for if I decided to do this? It's not necessary at this time, but I'm planning for w...

LINQ 2 SQL Query ObjectDisposed Exception...

This one i had today is a strange one. I have this query in an assembly method. public Order[] SelectAllOrders() { Order[] orders; using (MyDataContext context = new MyDataContext()) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<Order>(order => order.OrderDetails...

LINQ to SQL: First call

I'm using LINQ to SQL to access the database (SQL Server 2005). The first call takes up to 10 seconds to retrieve the data, a second call takes less than a second. What can be done to improve the performance of the first call to the database? The database action happens in the controller of a asp.net mvc application. Thanks ...

What is the best way to transparently log changes to objects when using LINQ-to-SQL?

I keep track of all changes that are made to objects so that the user can view and rollback to any previous version of any item in the database. The history database table looks like this: Item | ItemId | Field | WhenChanged | OldValue | NewValue customer | 6 | LastName | 2009-12-31 13:00:04 | Sanders | Sanders-...

Randomized Linq2SQl query that's return too heavy SQL

I use the following to implement Random ordered results in Linq2SQL: partial class OffertaDataContext { [Function(Name = "NEWID", IsComposable = true)] public Guid Random() { throw new NotImplementedException(); } } In the following query: IEnumerable<Enquirys> visibleOnSite = En...

How to best map database-aware entity types between application layers

I have an ASP.NET MVC app with a primitive repository layer that currently serves LINQ to SQL entities back to the controllers which then send them to the views. I now want to start using some domain-centric objects in place of my LINQ to SQL entities, and I have been using AutoMapper to help accomplish some of this. For simple propert...

OneToOne relation (cardinality) in LINQ to SQL with SQLMetal

Is there any possibility to set OneToOne relation (cardinality) when generate dbml with SQLMetal? By default dbml schema generated with the OneToMany relation. ...

Linq to Sql Mapping

When I modify the structure of the table in Sql Server ,won't it be automatically reflected in the "Dbml" Layout designer ?Each and every time i have to delete the tables in "dbml' layout designer and drag the table from sql server. ...

When can I dispose of my DataContext?

Take the following example: MyDataContext context = new MyDataContext(); // DB connection established. MyTableRecord myEntity = myDataContext.Table.FindEntity(12345); // retrieve entity Say my entity has relationships with other tables which I would access via foreach (var record in MyEntity.RelatedTable) Do I need to keep my DataC...

How can I access an object property set within an initializer?

I don't know if this is even possible, but how can I access an object property that has be set within an initializer so that I can use it to set another property within the same initializer? Here's what I'm trying to do: var usersWithCount = users .AsEnumerable() .Select( u => new User() { UserId = u.UserId, Us...

LINQ to SQL with too many records for memory

A lot of the LINQ to SQL that I've been doing involves reading data from a table and then calling the ToList() extension method and using the data in memory. However, I now want to use LINQ to SQL to process more records that can fit in memory. This is the pattern that I've come up with so far: int skip = 0; IList<Record> records = new ...

Is it possible to select data while a transaction is occuring?

I am using transactionscope to ensure that data is being read to the database correctly. However, I may have a need to select some data (from another page) while the transaction is running. Would it be possible to do this? I'm very noob when it comes to databases. I am using LinqToSQL and SQL Server 2005(dev)/2008(prod). ...