开放源码的对象关系映射工具ORM.NET 插入数据 Insert/Update Data
DataManager.New[Object] 创建数据记录
下面的代码片断创建一笔学生记录,然后把它保存到数据库中。
DataManager dm = new DataManager(Config.Dsn); //assign a new object reference Student s = dm.NewStudent(); s.FirstName = “Mike”; s.LastName = “Smith”; dm.CommitAll();
如果是主从关系的数据表,Student和Contact, ORM.NET会帮助处理好它们之间的外键引用关系
DataManager dm= new DataManager(Config.Dsn); Student s = dm.NewStudent(); s.FirstName = "Tim"; s.LastName = "Brown"; s.Contact = dm.NewContact(); s.Contact.Address1 = "555 Main Street"; s.Contact.Address2 = "Apt 6"; s.Contact.City = "Oakland"; s.Contact.State = "CA"; s.Contact.PostalCode = "80304"; s.Contact.Country = "US"; // create both records and maintain the relationship between them in database dm.CommitAll();
增加子对象 Add[ChildObject] Methods
通过Add[Child]方法,可以新加子对象,在CommitAll时,ORM.NET会自动维护它们的主外键关系。
DataManager dm= new DataManager(Config.Dsn); // Create new Student object Student student = dm.NewStudent(); student.FirstName = "Cherry"; student.LastName = "Jackson"; // Create a new contact contact contact = dm.NewContact(); contact.Address1 = "55 6th Avenue"; // add other contact information.. // Add the new Student to the new Contact object contact.AddStudent(student); dm.CommitAll();
New[ChildObject] methods
// find an existing Contact record dm.QueryCriteria.And(JoinPath.Contact.Columns.Address1,"123 Main Street"); Contact contact = dm.GetContact(FetchPath.All); Student student = contact.NewStudent(); // create a new Child object - Student student.firstname = "Tom"; student.LastName = "Smith"; //CommitAll() will create a new Student object and relate it to the Contact object dm.CommitAll();
如果对象已经在内存中,可以通过NewChild方式,创建新的子对象,在调用CommitAll时,会保存到数据库中。
Using Require Setting on New[Object] Methods for Column Properties
可以通过在ORM.NET Object Browser中设定属性FirstName为Required,必须输入。与此同时,NewStudent方法签名也会变化,以用于传入FirstName的值。
用于新加新对象的代码如下
//FirstName is now required to create new Student object Student s = dm.NewStudent("Mike"); //add a LastName value as well if you like... s.LastName = Smith; dm.CommitAll();
第一个参数Mike,就是FirstName。一般,只需要将主键字段列设定为Required即可。
Using Require Parent Object New[Object] Methods on Child records
Required属性也可以用在子对象的创建过程中,如下图所示
经过上图所示的设置,在NewObject中的一个方法签名,cocntact是必须输入的。
DataManager dm = new DataManager(Config.Dsn); // create a new contact object with some information Contact contact = dm.NewContact(); contact.Address1 = "123 Require Parent"; contact.City = "Boulder"; contact.Country = "US"; // Contact object is now required Student student = dm.NewStudent(contact); student.FirstName = "John"; student.LastName = "Glass"; // Create the new Student and related Contact record dm.CommitAll();
属性与它的父对象可以同时设置为Required,像下面的代码这样
// A FirstName property and Contact object are now required to create a new Student object
Contact = dm.GetContact();
Student student = dm.NewStudent(“Tom”,contact);
更新数据
DataManager dm = new DataManager(Config.Dsn); dm.QueryCriteria.Clear(); dm.QueryCriteria.And(JoinPath.Teacher.Columns.FirstName,"Tom",MatchType.Exact); Teacher teacher = dm.GetTeacher(FetchPath.Teacher); teacher.FirstName = "Thomas"; teacher.LastName = "Franklin"; dm.CommitAll(); // Will perform required update operation as a transaction
CommitAll方法会生成下面的SQL语句
BEGIN TRANSACTION UPDATE[Teacher] SET [LastName] = 'Franklin', [FirstName] = 'Thomas' WHERE ID = 10008 IF @@ERROR <> 0 BEGIN ROLLBACK TRANSACTION RETURN END COMMIT TRANSACTION
DataManager.CommitAllDebug
如果是调试程序,可以用CommitAllDebug生成所有SQL语句,但不会把SQL提交到数据库中执行。
dm.QueryCriteria.Clear(); dm.QueryCriteria.And(JoinPath.Teacher.Columns.FirstName,"Tom", MatchType.Exact); Teacher teacher = dm.GetTeacher(FetchPath.Teacher); teacher.FirstName = "Thomas"; teacher.LastName = "Franklin"; // Will NOT perform any changes but will generate the SQL statement(s) that would be // used in the Output window dm.CommitAllDebug();
这时,可以到Output窗口中查看生成SQL语句,以检测逻辑是否正确。
如果是调用DataManager.CommitAll,则可以通过调用它的属性LastCommitText来获取最后一次事务的SQL语句
DataManager dm = new DataManager(Config.Dsn); dm.QueryCriteria.Clear(); dm.QueryCriteria.And(JoinPath.Teacher.Columns.FirstName,"Tom",MatchType.Exact); Teacher teacher = dm.GetTeacher(FetchPath.Teacher); teacher.FirstName = "Thomas"; teacher.LastName = "Franklin"; dm.CommitAll(); // ouput generated SQL statement to console Console.WriteLine("OUTPUT SQL " + dm.LastCommitText);