Getting Started

Fluent Linq to SQL allows you to define the mappings for your classes using a fluent interface and lambda expressions rather than attributes or XML. The first thing you will need to do is add a reference to FluentLinqToSql.dll.

Creating a Mapping

Imagine you have a Customer object defined like this:

public class Customer {
   public int Id { get; set; }
   public string Forename { get; set; }
   public string Surname { get; set; }
}

To map this class to a table in the database you would create a mapping class that inherits from Mapping<T>:

namespace Demo {
	using FluentLinqToSql;

	public class CustomerMapping : Mapping<Customer> {
		public CustomerMapping() {
			Identity(x => x.Id);
			Map(x => x.Surname);
			Map(x => x.Forename);
		}
	}
}

This would create a mapping to a table called Customer and specify that the Id property should mapped to a column in the table called Id as an auto-incrementing primary key and that the Surname and Forename properties should be mapped to columns called Surname and Forename respectively.

Note that the Identity is just a shortcut to:

Map(x => x.Id).PrimaryKey().DbGenerated();

Customising table and column names

By default, the above mapping would be mapped to a table called Customer. You can override this by calling the Named method:

public class CustomerMapping : Mapping<Customer> {
	public CustomerMapping() {
		Named("Customers");
		//Column mappings...
	}
}

Column names can also be customised by calling the Named method:

Map(x => x.Surname).Named("LastName");

Creating the MappingSource

Once you have all your classes mapped, you will need to tell Linq to Sql how to find them. This can be done by using the FluentMappingSource.

var fluentMappings = new FluentMappingSource("MyDatabaseName");

Once created, you can add mappings by calling the AddMapping method:

var fluentMappings = new FluentMappingSource("MyDatabaseName");
fluentMappings.AddMapping(new CustomerMapping());

Rather than adding each mapping individually, you can use the AddFromAssembly or AddFromAssemblyContaining methods to automatically load all of the mappings from a particular assembly using reflection:

var fluentMappings = new FluentMappingSource("MyDatabaseName");
fluentMappings.AddFromAssemblyContaining<CustomerMapping>();

Next, you need to convert the fluent mappings to a Linq to Sql XmlMappingSource by calling the CreateMappingSource method. This object can then be passed to your DataContext:

var fluentMappings = new FluentMappingSource("MyDatabaseName");
fluentMappings.AddFromAssemblyContaining<CustomerMapping>();

var mappingSource = fluentMappings.CreateMappingSource();
var dataContext = new DataContext("MyConnectionString", mappingSource);

Note The mapping source only be created once at the start of your application. You can pass the same mapping source instance to all of your DataContexts. An easy way to achieve this is to create your own custom DataContext subclass that handles the caching of the mapping source for you:

using System.Data.Linq;
using System.Data.Linq.Mapping;
using FluentLinqToSql;

public class MyDataContext : DataContext {
	private static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
	private static readonly MappingSource mappings;
 
	static MyDataContext() {
		mappings = new FluentMappingSource("MyDatabase")
			.AddFromAssemblyContaining<CustomerMapping>()
			.CreateMappingSource();
	}
	public MyDataContext() : base(connectionString, mappings) {
		
	}
}

Note that you can chain the methods on the FluentMappingSource.

Last edited Sep 6, 2009 at 1:38 PM by JeremyS, version 2

Comments

No comments yet.