[LINQ TO SQL]使用LINQ TO SQL创建数据库

这篇博客将介绍如何使用LINQ TO SQL来创建数据库,以及如何映射Table之间的主外键关系。

我们的数据库表关系如下:

Province与City之间1:M,City与Area之间1:M的关系。

下面就通过LINQ TO SQL来创建这样的数据库,以Province表为例,

Province Table:

using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ImportAreaInfoApp.Models
{
    [Table(Name = "Province")]
    class Province
    {
        [Column(IsPrimaryKey = true, Name = "Id", IsDbGenerated = true, CanBeNull = false)]
        public int Id { get; set; }

        [Column]
        public string Name { get; set; }
    }
}

1). 引用System.Data.Linq.Mapping;

2). 给Province类添加Table属性,如果数据库表名与类名一致,可以不设置Table的Name值,会默认使用类名作为数据表的名字;

3). 数据表中列通过Column指定,如果该列为主键,需要设置IsPrimaryKey=true,同样的如果列名与字段名一致,Name可以不指定。

City Table:

using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ImportAreaInfoApp.Models
{
    [Table(Name = "City")]
    class City
    {
        [Column(IsPrimaryKey = true, Name = "Id", IsDbGenerated = true, CanBeNull = false)]
        public int Id { get; set; }

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

Area Table:

using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ImportAreaInfoApp.Models
{
    [Table(Name = "Area")]
    class Area
    {
        [Column(IsPrimaryKey = true, Name = "Id", IsDbGenerated = true, CanBeNull = false)]
        public int Id { get; set; }

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

到这里,数据表就创建完了。下面创建数据库

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

namespace ImportAreaInfoApp.Models
{
    [Database(Name = "ExpressDb")]
    class ExpressDbContext : DataContext
    {
        private const string ConnectionString = @"Data Source=HW-WPF-SER\SQLEXPRESS;Initial Catalog=ExpressDb;Integrated Security=True;";

        public Table<Province> Provinces;

        public Table<City> Cities;

        public Table<Area> Areas;

        public ExpressDbContext()
            : base(ConnectionString)
        {

        }
    }
}

1). 数据库类需要继承DataContext,并且类名需要添加Database属性,如果数据库名称与类名不一致,需要制定Name值;

2). 数据库所关联的表需要通过Table<Table Class> Tables指出来;

3). 在程序中我们可以通过ExpressDbContext类来操作所属的数据表完成CRUD操作。

创建数据库,

using (ExpressDbContext dbContext = new ExpressDbContext())
{
    if (dbContext.DatabaseExists())
    {
        dbContext.DeleteDatabase();
    }

    dbContext.CreateDatabase();
}

但是数据表之间的关系没有映射。以Province和City为例,对City类做如下修改,

using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ImportAreaInfoApp.Models
{
    [Table(Name = "City")]
    class City
    {
        [Column(IsPrimaryKey = true, Name = "Id", IsDbGenerated = true, CanBeNull = false)]
        public int Id { get; set; }

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

        [Column(Name = "Province")]
        private int? _provinceId;

        private EntityRef<Province> _province = new EntityRef<Province>();

        [Association(Name = "FK_City_Province", IsForeignKey = true, Storage = "_province", ThisKey = "_provinceId")]
        public Province Province
        {
            get { return _province.Entity; }
            set { _province.Entity = value; }
        }
    }
}

1). 创建一个int?型的私有变量来保存ProvinceId;

2). 创建EntityRef<Province>私有变量来保存City关联的Province;

3). 通过Association来标识关联性;

相应的,对Province类也要做修改,

using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ImportAreaInfoApp.Models
{
    [Table(Name = "Province")]
    class Province
    {
        [Column(IsPrimaryKey = true, Name = "Id", IsDbGenerated = true, CanBeNull = false)]
        public int Id { get; set; }

        [Column]
        public string Name { get; set; }

        private EntitySet<City> _cities = new EntitySet<City>();

        [Association(Name= "FK_City_Province", Storage = "_cities", OtherKey = "_provinceId", ThisKey = "Id")]
        public ICollection<City> Cities
        {
            get { return _cities; }
            set { _cities.Assign(value); }
        }
    }
}

1). 添加EntitySet<City>私有变量来持有Province相关联的City;

2). 通过Association来完成外键关系匹配;

经过上述代码的修改Province与City之间的1:M关系就建立了。

感谢您的阅读。

posted @ 2016-11-24 18:14  Yang-Fei  阅读(2089)  评论(1编辑  收藏  举报