Wednesday, March 17, 2010

Manual definition of dependent tables with LinqToSql

The following is a code example of how to declare dependent tables in C# and generate and populate them with LinqToSql [it will generate the tables Parent(Id, Name, ChildId) and Child(Id, Name)]:

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

namespace ManualLinqToSql
{
   class Program
   {
      static void Main(string[] args)
      {
         const string connectionString =
            @"Server=.\SqlExpress;Database=LinqTest;Integrated Security=true;";
         TestRepository repository =
            new TestRepository(connectionString);
         try
         {
            repository.CreateDatabase();
            repository.Add("A", "A1");
            repository.Add("B", "B1");
            Console.WriteLine("OK");
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex);
         }
         Console.ReadKey();
      }
   }

   public class TestRepository
   {
      private readonly TestDataContext _dataContext;

      public TestRepository(string connectionString)
      {
         _dataContext = new TestDataContext(connectionString);
      }

      public void Add(string parentName, string childName)
      {
         var parent = new Parent(parentName, childName);
         _dataContext.ParentTable.InsertOnSubmit(parent);
         _dataContext.SubmitChanges();
      }

      public void CreateDatabase()
      {
         _dataContext.DeleteDatabase();
         _dataContext.CreateDatabase();
      }
   }

   public class TestDataContext : DataContext
   {
      public TestDataContext(string connectionString)
         : base(connectionString)
      {
      }

      public Table<Parent> ParentTable
      {
         get { return GetTable<Parent>(); }
      }

      public Table<Child> ChildTable
      {
         get { return GetTable<Child>(); }
      }
   }

   [Table]
   public class Parent
   {
      private EntityRef<Child> _child;

      public Parent()
      {
      }

      public Parent(string name, string childName)
      {
         Name = name;
         Singleton = new Child { Name = childName };
      }

      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int Id { get; set; }

      [Column(CanBeNull = false)]
      public string Name { get; set; }

      [Association(Storage = "_child", ThisKey = "ChildId", IsForeignKey = true)]
      public Child Singleton
      {
         get
         {
            return _child.Entity;
         }
         set
         {
            _child.Entity = value;
            ChildId = value.Id;
         }
      }

      [Column(UpdateCheck = UpdateCheck.Never)]
      private int ChildId { get; set; }
   }

   [Table]
   public class Child
   {
      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int Id { get; set; }

      [Column(CanBeNull = false)]
      public string Name { get; set; }
   }

   public void Detach()
   {
      _child = default(EntityRef<Child>);
   }
}

No comments: