对象关系映射,开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start

ORM.NET是一个开源的ORM工具,用它来替代ADO.NET访问数据库,快速,方便,当前的版本是1.7。 这篇入门教程将引导如何用ORM.NET开发数据库类型的应用程序。

1  创建数据库 Create your database schema

ORM.NET是数据库驱动的(database-driven)的ORM开发工上具,请先用SQL Enterprise Manager或SQL Server Management Studio创建数据库,定义表,列及其关系
数据库的脚本schema有以下要求
1) 表需要定义主键,以唯一表示实体与数据行的映射关系。
2) 需要定义表与表之前的关系。比如,一对多,主从映射关系。
此外,ORM.NET推荐以下的数据库设计方式
1) 不要在表名中用空格(spaces)或是特殊字符,如果有定义,则在映射为实体时,会使用下划线取代。
2) 使用单数(singular),而不是复数。比如Customer而不是Customers。因为对于复数,会产生Collection来替代。
image开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start对象关系映射

2  连接到数据库 Connect to your database

当创建好数据库之后,启动ORM.NET设计工具,连接到数据库
imageimage开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start对象关系映射
到目前为止,ORM.NET只支持SQL Server 数据库。
ORM.NET连接到数据库之后,会检测无效的命名,没有主键的表。

3  修改数据对象和属性设置 Modify Data Object and Property settings

也可以不用任何修改,直接跳到下一步生成数据访问层。如果要做定制,可以做到
1)修改被映射的表和列的对应的对象名
2) 修改列属性的主从对象的作用域(scope)
3) 设置列的属性,比如设置为只读,或可以更新
4) 设置对象被创建时的必须的属性,通常是主键
 

4 生成数据访问层 Generate Data Layer

对表或列做出适当的修改后,就可以生成数据访问层,请点击按钮Generate Data Layer。
imageimageimage开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start对象关系映射
此时,ORM.NET会验证数据库脚本和命名的正确性,以保证能生成正确的对象及其关系。 如果验证没有错误,会提示保存项目的目录,准备生成解决方案文件。
imageimageimageimage开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start对象关系映射
 

5 编译生成的数据访问类型库  Compile generated Data Layer solution

打开生成的解决方案文件,直接编译即可。
imageimageimageimageimage开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start对象关系映射
 

6 开发应用程序  Develop your application

到这里,才开始编写应用程序代码,前面的步骤是在运用工具,生成必须的项目和文件。
ORM.NET会生成两个项目,一个以项目名称命名的类库,另一个是TestApp控制台应用程序,它的代码如下
using System; using System.Diagnostics; using System.Configuration; using System.Collections; using System.Data.SqlTypes; using OrmLib; using OleroTrainingBiz; namespace TestApp { /// /// This test app can be used to test your new Data Layer object. /// Please make sure that the test app is set as the startup Project. /// class TestApp { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // DataManager dm = new DataManager(Config.Dsn); // Start coding! } }
}
如果你已经熟悉了ORM.NET的开发接口,可以删除TestApp项目,进行代码开发。 下面列举几个例子,以展现ORM.NET的数据访问接口,以了解它是如何读写数据库。
例1 使用ORM.NET查询语法执行查询
// EXAMPLE 1 - Find all students enrolled in Biology 100 dm.QueryCriteria.And(JoinPath.Student.Enrollment.Schedule.Course.Columns.ClassName,"Biology 100"); // Return this information as a Collection of Student objects with related Enrollment, Schedule and //Course information for each Student StudentCollection students = dm.GetStudentCollection(FetchPath.Student.Enrollment.Schedule.Course); // iterate through the collection of students foreach (Student s in students) Console.WriteLine("Students in Biology: " + s.LastName);
例2 简单的INSERT命令,插入新的数据
// EXAMPLE 2 - Insert a new Teacher with Contact information Teacher teacher = dm.NewTeacher(); teacher.FirstName = "Edward"; teacher.LastName = "Blake"; teacher.Contact = dm.NewContact(); teacher.Contact.Address1 = "123 Main Street"; teacher.Contact.City = "Boulder"; teacher.Contact.State = "Colorado"; teacher.Contact.Country = "US"; // Peform SQL operation(s) dm.CommitAll();
例3 删除表及其关系
//EXAMPLE 3 - Delete the New Teacher and related Contact info dm.QueryCriteria.Clear(); // clear any previous queries dm.QueryCriteria.And(JoinPath.Teacher.Columns.FirstName,"Edward",MatchType.Exact); dm.QueryCriteria.And(JoinPath.Teacher.Columns.LastName,"Blake",MatchType.Exact); // Now retrieve Teacher and Contact record from the query above teacher = dm.GetTeacher(FetchPath.Teacher.Contact); teacher.Contact.Delete(); // mark the Parent Teacher Contact for deletion teacher.Delete(); // then mark Teacher for deletion // Peform SQL operation(s) dm.CommitAll();
Tags: 

延伸阅读

最新评论

发表评论