Tag archive for 'linqtosql'

26
Jun

CodeSmith template to generate LINQ To SQL Data Context

If you are interested in what LINQ to SQL generates and don't have Orcas installed or available right now but use
CodeSmith try the following template to generate very similar code.

The primary difference is that this writes out the System types rather than the C# aliases
(e.g. System.Int32 instead of int) but that could easily be changed but is binary compatible
and otherwise almost identical to the source.

Download LINQ to SQL template (CodeSmith) (4 KB)

<%@ CodeTemplate Src="LinqFunctions.cs" Inherits="LinqFunctions" Language="C#" TargetLanguage="C#"
  Description="Generates a data context and entities for given tables." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Data" %>
<%@ Property Name="DataContextClassName" Type="System.String" Category="Naming" Optional="True"Description="Name of the data context class to generate." %>
<%@ Property Name="Namespace" Type="System.String" Category="Naming" Optional="True" Description="Namespace for the data context class to generate." %>
<%@ Property Name="SourceTables" Type="SchemaExplorer.TableSchemaCollection" Category="Connection"Description="Tables to be mapped." %>
<%SchemaExplorer.DatabaseSchema database = SourceTables[0].Database;
string className = (String.IsNullOrEmpty(DataContextClassName)) ? DatabaseName(database) + "DataContext" : DataContextClassName;
Dictionary<TableSchema, List<TableKeySchema>> reverseForeignKeys = new Dictionary<TableSchema, List<TableKeySchema>>();
foreach(TableSchema table in SourceTables) {
    foreach(TableKeySchema keySchema in table.ForeignKeys) {
        if (reverseForeignKeys.ContainsKey(keySchema.PrimaryKeyTable)) {
            reverseForeignKeys[keySchema.PrimaryKeyTable].Add(keySchema);
        }
        else {
            List<TableKeySchema> keySchemas = new List<TableKeySchema>();
            keySchemas.Add(keySchema);
            reverseForeignKeys.Add(keySchema.PrimaryKeyTable, keySchemas);
        }
    }
}%>//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     CodeSmith template DeLINQuent.cst v0.1
//     Generated by <%=CurrentUserName()%> at <%=DateTime.Now%>
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace <%=Namespace%>{
    public partial class <%=className%> : global::System.Data.Linq.DataContext
    {
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public <%=className%>(string connection) :
                base(connection)
        {
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public ProjectDataContext(global::System.Data.IDbConnection connection) :
                base(connection) {
        }
<%    foreach(TableSchema table in SourceTables) {
        string entityName = EntityName(table);
        string propertyName = TableName(table);%>
        public global::System.Data.Linq.Table<<%=entityName%>> <%=propertyName%> {
            get {
                return this.GetTable<<%=entityName%>>();
            }
        }
<%     } %>
    }<%    foreach(TableSchema table in SourceTables) {
        string entityName = EntityName(table);%>
    [global::System.Data.Linq.Table(Name="<%=table.FullName%>")]
    public partial class <%=entityName%> :global::System.Data.Linq.INotifyPropertyChanging,global::System.ComponentModel.INotifyPropertyChanged
    {
<%    foreach(ColumnSchema column in table.Columns) {%>
        private <%=NullableSystemType(column)%> _<%=PropertyName(column)%>;
<%    }
      if (table.ForeignKeys.Count > 0) {%>
<%        foreach(TableKeySchema keySchema in table.ForeignKeys) {
            string foreignEntityName = EntityName(keySchema.PrimaryKeyTable);%>
        private global::System.Data.Linq.EntitySet<<%=foreignEntityName%>> _<%=foreignEntityName%>;<%
          }
      }
      if (reverseForeignKeys.ContainsKey(table)) {%>
<%        foreach(TableKeySchema keySchema in reverseForeignKeys[table]) {            string propertyName = TableName(keySchema.ForeignKeyTable);%>
        private global::System.Data.Linq.EntityRet<<%=EntityName(keySchema.ForeignKeyTable)%>> _<%=propertyName%>;
<%        }
      } %>
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public <%=entityName%>() {
        }
<%    foreach(ColumnSchema column in table.Columns) {
        string propertyName = PropertyName(column);%>
        [global::System.Data.Linq.Column(Storage="_<%=propertyName%>", Name="<%=column.Name%>", DBType="<%=AttributeColumnDbType(column)%>"<%
            if (column.IsPrimaryKeyMember) { %>, IsPrimaryKey=true<% }
            if (!column.AllowDBNull) { %>, CanBeNull=false<% } %>)]
        public <%=NullableSystemType(column)%> <%=propertyName%> {
            get {
                return this._<%=propertyName%>;
            }
            set {
                if (this._<%=propertyName%> != value) {
                    this.OnPropertyChanging("<%=propertyName%>");
                    this._<%=propertyName%> = value;
                    this.OnPropertyChanged("<%=propertyName%>");
                }
            }
        }

<%    } %>
<%    foreach(TableKeySchema keySchema in table.ForeignKeys) {
        string propertyName = EntityName(keySchema.PrimaryKeyTable);
        string tableName = TableName(keySchema.PrimaryKeyTable);%>
        [global::System.Data.Linq.Association(Name="<%=keySchema.Name%>",
            Storage="_<%=propertyName%>", OtherKey="<%=AttributeColumnList(keySchema.ForeignKeyMemberColumns)%>",
            ThisKey="<%=AttributeColumnList(keySchema.PrimaryKeyMemberColumns)%>", IsForeignKey=true)]
        public <%=propertyName%> <%=propertyName%> {
            get {
                return this._<%=propertyName%>.Entity;
            }
            set {
                if (this._<%=propertyName%>.Entity != value) {
                    this.OnPropertyChanging("<%=propertyName%>");
                    if ((this._<%=propertyName%>.Entity != null)) {
                        <%=propertyName%> temp = this._<%=propertyName%>.Entity;
                        this._<%=propertyName%>.Entity = null;
                        temp.<%=TableName(table)%>.Remove(this);
                    }
                    this._<%=propertyName%>.Entity = value;
                    if ((value != null)) {
                        value.<%=TableName(table)%>.Add(this);
                    }
                    this.OnPropertyChanged("<%=propertyName%>");
                }
            }
        }
<%    } %>
<%    if (reverseForeignKeys.ContainsKey(table)) {
        foreach(TableKeySchema keySchema in reverseForeignKeys[table]) {
            string propertyName = TableName(keySchema.ForeignKeyTable);%>
        [global::System.Data.Linq.Association(Name="<%=keySchema.Name%>",
            Storage="_<%=propertyName%>", OtherKey="<%=AttributeColumnList(keySchema.PrimaryKeyMemberColumns)%>",
            ThisKey="<%=AttributeColumnList(keySchema.ForeignKeyMemberColumns)%>")]
        public IEnumerable<<%=EntityName(keySchema.ForeignKeyTable)%>> <%=propertyName%> {
            get {
                return this._<%=propertyName%>;
            }
        }

<%             }
        } %>
        public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanging;
        public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        protected void OnPropertyChanging(string propertyName) {
            if (this.PropertyChanging != null)
                this.PropertyChanging(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        protected void OnPropertyChanged(string propertyName) {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
<%     } %>
}

[)amien

20
Jun

Discarding new entity objects in LINQ to SQL beta 1

An unusual bug in .NET Framework 3.5 beta 1 that relates to creating a new entity object in LINQ to SQL has been getting in my way.

Bug description

When you create a new entity object it is not automatically added to the database unless you either attach it to the table or create an association to another existing entity.

The problem arises when you decide to discard the new object instead of persisting it. Setting the property of the association(s) to null should achieve this and the LINQ to SQL designer code shows that it removes the new object from the association.

When SubmitChanges is called it is apparent that the new object has not been discarded but is in fact attempting to persist to the database.

Reproduction

  1. Create a new C# Console project
  2. Add a new a LINQ to SQL file and name it Northwind
  3. Use Server Explorer to drag Orders and Customers tables from a Northwind database connection
  4. Paste the following code snippet into the static main method
  5. Run the program
  6. Examine the Orders table and notice a new a record consisting of null fields
NorthwindDataContext dc = new NorthwindDataContext();
Customer alfredCustomer = dc.Customers.Single(c => c.CustomerID == "ALFKI");
Order newOrder = new Order();
newOrder.Customer = alfredCustomer;
newOrder.Customer = null;
dc.SubmitChanges();

Workaround

It would appear there is at least a temporary workaround which is to force the association that was modified to reload. You can do this by calling the associations Load method after the entity has been de-associated but before SubmitChanges.

In our reproduction this would therefore be:

newOrder.Customer = null;
alfredCustomer.Orders.Load();
dc.SubmitChanges();

Calling Load does not disrupt other still-valid associations that have not yet persisted.

Curiously accessing the Count property of the association also causes the problem to disappear because the EntitySet<T> class that is responsible for representing these one-to-many associations calls Load every time you access Count. This is a potential performance hit as accessing a Count property is considered to be a fast operation that does not go off and rebuild collections.

Here's hoping that Microsoft fix those two bugs before release and ideally before beta 2.

[)amien

11
Jun

LINQ to SQL NullReferenceException on SubmitChanges

I've been busy working on some LINQ to SQL (formerly DLINQ) apps that have been going well bar a NullReferenceException thrown at me from deep in the bowels of LINQ and it's change tracker. A cursory glance in debug showed none of the properties that represent columns were null...

The null was actually a collection property that represented a one-to-many relationship that was automatically created using the foreign key constraints in the database. Normally this is set to an empty EntitySet however I was failing to call the default constructor from my new useful constructor.

The moral of the story is always call the default constructor with this(), especially when extending generated code. So much for persistence ignorance.

public partial class Invoice
{
    public Invoice(int number) : this() {
        this.number = number;
    }
}

[)amien

16
May

LINQ to SQL details, issues and patterns

LINQ to SQL (formely called DLINQ) is a simple object-relational mapper (ORM) scheduled for .NET Framework 3.5/Visual Studio 2007 (Orcas).

On projects with new data I'm keen on keeping the tables and classes as similar as possible and so the limited functionality of LINQ to SQL really appeals to me.

Sure for larger applications or where legacy data does not inspire great classes then a more advanced ORM with better mapping facilities can outweigh the disadvantages and learning curve imposed by another layer of abstraction. Microsoft have that covered with LINQ to Entities which will ship post-Orcas because of the immaturity of the the designer and advanced scenarios.

Whilst prototyping I've come up with a few oddities that deserve documenting somewhere. Here they are in not-quite-FAQ format.

What SQL is this query or operation generating?

Set the Log property of your DataContext object to Console.Out.

What objects are generated and how do I extend them?

MyDataContext : DataContext

Represents a users view of the database and is generated by designer/SQLMetal exposing a property for each table being mapped.

Can be extended by writing your own partial class or sub classing.

MyEntity / T

Represents an entity for each row in the associated table and is generated by designer/SQLMetal exposing a property for each field being mapped which is also decorated with attributes that specify the underlying field name and type.

Can be extended by writing your own partial class, sub classing or providing a superclass.

No classes are generated for the tables themselves, instead the generic class Table<T> uses the attribute information from the entity objects (cached via various metadata classes such as MetaTable).

What patterns does LINQ to SQL use?

Thankfully ActiveRecord is ignored and a more flexible approach is used. It basically provides one class for the database and one for each type of entity (because of inheritance, there could be more than one type of entity per table).

I've yet to delve deep enough into the classes and patterns to find good matches but a brief look reveals the following possibilities:

Registry - System.Data.Linq.DataContext
Unit of Work - System.Data.Linq.DataContext
Identity Map - System.Data.Linq.IdentityManager
Table Data Gateway - System.Data.Linq.Table<T>
Query Object - System.Data.Linq.DataQuery<T>
Metadata Mapping - System.Data.Linq.Provider.Meta*, System.Data.Linq.*Attribute, System.Data.Linq.Mapping.*

How do I persist changes?

Call the SubmitChanges method on your DataContext object.

What causes the error "Row not found or changed" when submitting changes?

By default LINQ to SQL creates UPDATE statements that include every field as it was when read in the WHERE clause and not just the primary key as you might expect. The upshot is that if the record has changed since it was loaded it will not find it.

However, there are also a couple of scenarios where it won't find the record even if it hasn't changed:

  • If the data range/precision of the .NET type can't exactly hold the value in the SQL table (e.g. DateTime)
  • If the table schema doesn't exactly match the mapping (e.g. a column's nullability changing)

If in doubt remove the table from your LINQ schema diagram and drag a fresh copy back in from your database using the Server Explorer pane.

Why does the OnPropertyChanging event use the PropertyChanged event handler delegate?

OnPropertyChanging does indeed seem to be using the PropertyChangedEvent and PropertyChangedEventArgs that OnPropertyChanged use.

Being that PropertyChangingEvent and PropertyChangingEventArgs exist I would assume this is a bug within the designer/SQLMetal in beta 2.

What database vendors are supported?

SQL Server 2000, 2005, 2008 and SQLCE are currently included.

Why do queries against a table return entities that do not match the query?

This occurs when entity objects in memory are out-of-step with data in the database.

The query is exectuted against the SQL server and for each matching record it either creates and entity object or uses the cached one it already has.

Therefore it is possible your result set will:

  • not include matching entities because the database indicates they do not (database changed or do not exist)
  • include entities that do not match because the database indicated they did (database changed or object changed)

Why is memory consumption so high?

LINQ to SQL tends to burn more memory than you might be expecting because of the multiple objects and aggressive caching in place.

Should I share a DataContext between users?

Web applications, web services and middle-tiers often share objects between potential users and requests. It is worth bearing in mind that a DataContext is not well suited to sharing between users because:

  • Entities are cached indefinitely
    • High memory use as every entity is eventually loaded to memory
    • Entities to be out-of-date if the data can be updated elsewhere (triggers, imports, background jobs)
  • DataContext.CommitChanges persists all objects changed via that DataContext
    • Difficult to determine a safe point to commit User A's completed entities without committing User B's incomplete entities
  • DataContext's Table<T> creation is not thread safe
    • Accessing a table for the first time on two threads could cause the DataContext to create one instance of a Table<T> whilst creating another for the same <T> and resulting in the latter being kept but the former being returned to one of the callee's.

A DataContext is therefore best suited for either a single-user application, per-session or per-request.

[)amien

06
Jan

LINQ in C# Web Applications

I'm a big fan of the Web Application type that was previously available as an add-on to Visual Studio 2005 but thankfully got promoted to a standard citizen with Service Pack 1.

So with a little more time on my hands lately I've been delving into the wonder that is LINQ - part of the forthcoming Orcas release of technologies.

For those who've been living under a rock LINQ is a set of extensions to .NET that let you perform queries on objects in much the same way you would do on a database with SQL (except the syntax is backwards by comparison).

Now while the LINQ Preview CTP installs LINQ projects for C# Class Libraries, Windows Applications and Console Applications it inconveniently misses-out Web Applications!

You can drop this ZIP file in your %UserProfile%\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual C# to gain a new ASP.NET LINQ Web Application project type for you to start with as often as you like as shown:

Window of the LINQ To SQL templates available

Or if you want to modify an existing Web Application simply:

  1. Open up the .csproj file in Notepad and replace
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    with
    <Import Project="$(ProgramFiles)\LINQ Preview\Misc\Linq.targets" />
  2. Add project references to:
    System.Data.DLinq
    System.Query
    System.Xml.XLinq

Hopefully more LINQ related posts as I get to grips with it.

[)amien