这次以一个简单的例子来实现自定义配置LINQ实体类。
1 use master
2 if exists(select * from sys.databases where [name] ='SchoolManager' )
3 drop database SchoolManager
4
5 CREATE DATABASE SchoolManager --数据库的真实名称
6 on primary --数据库的基本信息 primary 可选的 主要的
7 (
8 NAME = 'SchoolManager_data',--逻辑名称
9 FILENAME ='E:\资料工具\技术资料学习记录\another\SchoolManager_Mdata.mdf',--主数据库文件的目录
10 SIZE = 5mb, --数据库的初始大小
11 FILEGROWTH =5mb--自动增长的标准
12 )
13 log on --一个数据库必须要有的是一个主数据文件和一个日志文件
14 (
15 NAME='SchoolManager_log',
16 FILENAME = 'E:\资料工具\技术资料学习记录\another\SchoolManager_Ldata.ldf',
17 SIZE = 5mb ,
18 FILEGROWTH = 5mb
19 )
20 go
21
22 use SchoolManager
23 if exists (select * from sys.tables where [name]='MyClass')
24 drop table MyClass
25 create table MyClass
26 (
27 class_no varchar(25) primary key not null,---
28 class_name varchar(200) not null
29 )
30
31 if exists(select * from sys.tables where [name] = 'MyStudent')
32 drop table MyStudent
33 create table MyStudent
34 (
35 stu_no varchar(25) primary key not null,
36 stu_name varchar(50) not null,
37 stu_age int ,
38 stu_sex bit ,--true is female ,false is male
39 stu_address varchar(500) ,
40 stu_class_no varchar(25) not null --班级编号,外键
41 )
42 如若需要建立外键关系,可以使用下面语句。
43
44 ----建立外键关系
45 ALTER TABLE MyStudent ADD CONSTRAINT FK_mystudent_myclass
46 FOREIGN KEY (stu_class_no)
47 REFERENCES MyClass(class_no);
48 --删除外键
49 ALTER TABLE MyStudent DROP CONSTRAINT FK_mystudent_myclass
1 [Serializable()]
2 [Table(Name = "MyClass")]
3 ///<summary>
4 /// 班级 模型,对应MyClass表
5 ///</summary>
6 public class MyClass
7 {
8 private string _classNo;
9
10 [Column(CanBeNull = false, DbType = "varchar(25)", IsPrimaryKey = true
11 , Name = "class_no", Storage = "_classNo")]
12 ///<summary>
13 /// 班级编号
14 ///</summary>
15 public string ClassNo
16 {
17 get { return _classNo; }
18 set { _classNo = value; }
19 }
20
21 private string _className;
22
23
24 [Column(CanBeNull = false, DbType = "varchar(200)", Name = "class_name", Storage = "_className")]
25 ///<summary>
26 /// 班级名称
27 ///</summary>
28 public string ClassName
29 {
30 get { return _className; }
31 set { _className = value; }
32 }
33
34 private EntitySet<Student> _students =new EntitySet<Student>();
35
36 [Association(OtherKey = "MyClass", Storage = "_students", ThisKey = "ClassNo")]
37 ///<summary>
38 /// 学生集合
39 ///</summary>
40 public EntitySet<Student> Students
41 {
42 get { return _students; }
43 set { _students = value; }
44 }
45 }
1 [Serializable()]
2 [Table(Name = "MyStudent")]
3 ///<summary>
4 /// 学生 模型,对应MyStudent表
5 ///</summary>
6 public class Student
7 {
8 private string _stuNo;
9
10 [Column(CanBeNull = false, DbType = "varchar(25)", IsPrimaryKey = true
11 , Name = "stu_no", Storage = "_stuNo")]
12 ///<summary>
13 /// 学生编号
14 ///</summary>
15 public string StuNo
16 {
17 get { return _stuNo; }
18 set { _stuNo = value; }
19 }
20
21
22 private string _stuName;
23
24 [Column(CanBeNull = false, DbType = "varchar(200)", Name = "stu_name", Storage = "_stuName")]
25 ///<summary>
26 /// 学生姓名
27 ///</summary>
28 public string StuName
29 {
30 get { return _stuName; }
31 set { _stuName = value; }
32 }
33
34 private Sex _stuSex;
35
36 [Column(CanBeNull = false, DbType = "bit", Name = "stu_sex", Storage = "_stuSex")]
37 ///<summary>
38 /// 学生性别,true为女,false为男
39 ///</summary>
40 public Sex StuSex
41 {
42 get { return _stuSex; }
43 set { _stuSex = value; }
44 }
45
46 private Nullable<int> _stuAge;
47
48 [Column(DbType = "int", Name = "stu_age", Storage = "_stuAge")]
49 ///<summary>
50 /// 学生年龄,可为空
51 ///</summary>
52 public Nullable< int> StuAge
53 {
54 get { return _stuAge; }
55 set { _stuAge = value; }
56 }
57
58 private string _stuAddress;
59
60 [Column(DbType = "varchar(500)", Name = "stu_address", Storage = "_stuAddress")]
61 ///<summary>
62 /// 学生住址,可为空
63 ///</summary>
64 public string StuAddress
65 {
66 get { return _stuAddress; }
67 set { _stuAddress = value; }
68 }
69
70 private string _classNo;
71
72 [Column(Name = "stu_class_no", Storage = "_classNo",CanBeNull=false)]
73 public string ClassNo
74 {
75 get { return _classNo; }
76 set { _classNo = value; }
77 }
78
79 private EntityRef<MyClass> _myClass =new EntityRef<MyClass>();
80
81 [Association(IsForeignKey = true, Storage = "_myClass", ThisKey = "ClassNo")]
82 ///<summary>
83 /// 学生所在班级,
84 ///</summary>
85 public MyClass MyClass
86 {
87 /*这里需要重点注意*/
88 get { return _myClass.Entity; }
89 set { _myClass.Entity = value; }
90 }
91
92 }
1 [Database(Name = "SchoolManager")]
2 ///<summary>
3 /// 数据库对象
4 ///</summary>
5 public class SchoolManager : DataContext
6 {
7
8 //public SchoolManager()
9 // : base(@"E:\资料工具\技术资料学习记录\another\SchoolManager_Mdata.mdf")
10 //{
11 //}
12 //使用这种连接方式一直跑出无法连接的40错误,有谁可以指教。
13 public SchoolManager()
14 : base("server=localhost;uid=sa;pwd=sa;database=SchoolManager;")
15 {
16
17 }
18 public SchoolManager(string connection)
19 : base(connection)
20 {
21
22 }
23
24 ///<summary>
25 /// 班级表
26 ///</summary>
27 public Table<MyClass> MyClass ;
28
29 ///<summary>
30 /// 学生表
31 ///</summary>
32 public Table<Student> Student;
33 }