思归呓语

衣带渐宽终不悔,为伊消得人憔悴
随笔 - 409, 评论 - 2969, 引用 - 245

导航

关于

标签

每月存档

最新留言

广告

 

1.建立数据库

use master
go

create database Jasper
go

use Jasper
go

CREATE TABLE Users (
UserID int identity NOT NULL,
UserName nvarchar(40) NOT NULL,
Password nvarchar(20) NOT NULL,
EmailAddress nvarchar(40) NOT NULL,
LastLogon datetime,
PRIMARY KEY (UserID)
)
go

CREATE TABLE Posts (
PostID int identity NOT NULL,
Title nvarchar(100) NOT NULL,
Message ntext,
CreatedDate datetime,
ModifiedDate datetime,
UserID int NOT NULL,
PRIMARY KEY (PostID),
FOREIGN KEY (UserID) References Users(UserID)
)
go

2. 运行ipy命令行工具

C:\>ipy
IronPython 1.1 (1.1) on .NET 2.0.50727.1318
Copyright (c) Microsoft Corporation. All rights reserved.

3. 装载所需库

>>> import clr
>>> clr.AddReference("Microsoft.Jasper.CTP")
>>> from Microsoft.Jasper import *

4. 设置连接字符串,注意我们使用了默认的机制来通过实体框架动态生成对象

>>> connStr = "Provider='System.Data.SqlClient';Provider Connection String='Initial Catalog=Jasper;Data Source=.\SQLExpress;Integrated Security=True;';Generate Default EDM=True"

5. 生成动态上下文对象

>>> ctx = DynamicContext.CreateDynamicContext(connStr)

6. 看一下ctx对象的成员

>>> dir(ctx)

['AcceptAllChanges', 'AddObject', 'Attach', 'AttachTo', 'Connection', 'ContextSaved', 'ContextSaving', 'CreateDynamicContext', 'CreateKey', 'CreateQuery', 'DefaultContainerName', 'DeleteObject', 'Detach', 'Dispose', 'Equals', 'ExecuteDirectQuery', 'ExecuteFunction', 'ExecuteNativeMethod', 'Finalize', 'GetHashCode', 'GetObjectByKey', 'GetQuery', 'GetType', 'MakeDynamicType', 'MemberwiseClone', 'MetadataWorkspace', 'ObjectStateManager', 'Posts', 'QueryTimeout', 'Reduce', 'ReferenceEquals', 'Refresh', 'SaveChanges', 'ToString', 'TryGetObjectByKey', 'TryGetQuery', 'Users', '__class__', '__doc__', '__entityHelper', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '_posts', '_users', 'add_ContextSaved', 'add_ContextSaving', 'remove_ContextSaved', 'remove_ContextSaving']

里面有Users和Posts

7. 目前数据库还没有任何数据

>>> ctx.Users.Count()
0
>>> ctx.Posts.Count()
0

8. 让我们来生成一个User对象

>>> u = ctx.Users.Create()
>>> u.UserName = "joel_cool"
>>> u.Password = "abc123"
>>> u.EmailAddress = "joe@cool.com"
>>> u.LastLogon = DateTime.Now
Traceback (most recent call last):
File , line 0, in <stdin>##42
NameError: name 'DateTime' not defined
>>> u.LastLogon = System.DateTime.Now
Traceback (most recent call last):
File , line 0, in <stdin>##43
NameError: name 'System' not defined
>>> from System import *
>>> u.LastLogon = DateTime.Now
>>> ctx.SaveChanges()
1
>>> ctx.Users.Count()
1
>>> u.UserID
1

忘了引进System命名空间

9. 生成几个贴子对象

>>> p = ctx.Posts.Create()
>>> p.User = u
>>> p.Title = "abc"
>>> p.Message = "Hello world"
>>> p.CreatedDate = p.ModifiedDate = DateTime.Now
>>>

>>> p2 = ctx.Posts.Create()
>>> p2.User = u
>>> p2.Title = "def"
>>> p2.Message = "Jasper is fun"
>>> p2.CreatedDate = p2.ModifiedDate = DateTime.Now
>>> ctx.SaveChanges()
4
>>> ctx.Posts.Count()
2
>>> p.PostID
1
>>> p2.PostID
2
>>> u.Posts.Count()
2

10. 查询一个贴子,然后做修改

>>> p3 = ctx.Posts.FindByKey(2)
>>> p3
<Post object at 0x000000000000002B>
>>> p2
<Post object at 0x000000000000002B>
>>>注意p2和p3指向同一个对象ID
>>> p3.Message += ", yes, really fun"
>>> p3.ModifiedDate = DateTime.Now
>>> ctx.SaveChanges()
1
>>> p2.Message
'Jasper is fun, yes, really fun'
>>> p2.CreatedDate
<System.DateTime object at 0x000000000000002C [5/1/2007 8:15:16 PM]>
>>> p2.ModifiedDate
<System.DateTime object at 0x000000000000002D [5/1/2007 8:17:18 PM]>

11. 删除其中一个贴子

>>> p4 = u.Posts.FindByKey(1)
>>> ctx.Posts.Delete(p4)
True
>>> ctx.SaveChanges()
2
>>> ctx.Posts.Count()
1
>>> u.Posts.Count()
2

#!!!好像有问题,用户对象还需要从自己的贴子集合里删除贴子,是特性还是缺陷? 看样子这里需要一个用法模式

>>> p4.User
Traceback (most recent call last):
File , line 0, in <stdin>##106
File , line 0, in get_User##107
File dynamicAssembly1, line unknown, in get_User
File Microsoft.Jasper.CTP, line unknown, in GetRelatedReferenceValue
File System.Data.Entity.CTP, line unknown, in Load
SystemError: Unable to load the EntityReference because it is not attached to a
context.

#但这里p4指向的User已经出问题了,p3呢?

>>> p3.User
<User object at 0x000000000000002E>
>>> p3.User.UserName
'joel_cool'

12.把用户也删除吧

>>> ctx.Users.Delete(u)
True
>>> ctx.SaveChanges()
Traceback (most recent call last):
File , line 0, in <stdin>##116
File , line 0, in SaveChanges##49
File Microsoft.Jasper.CTP, line unknown, in SaveChanges
File System.Data.Entity.CTP, line unknown, in SaveChanges
File System.Data.Entity.CTP, line unknown, in SaveChanges
File System.Data.Entity.CTP, line unknown, in Update
File System.Data.Entity.CTP, line unknown, in Update
File System.Data.Entity.CTP, line unknown, in ValidateConstraints
SystemError: A relationship is being added or deleted from relationship set 'FK__Posts__UserID__7F60ED59'. Given cardinality constraints, a corresponding 'Posts' must also be added or deleted.
>>>

外键约束的关系,无法删除用户,把贴子也全删了!

>>> ctx.Posts.DeleteAll()
>>> ctx.SaveChanges()
3
>>> ctx.Users.Count()
0
>>> ctx.Posts.Count()
0
>>> u.Posts.Count()
2

打印 | 张贴于 2007-05-02 08:38:00 | Tag:ASP.NET/IIS  ADO.NET/SQL Server  IronPython/IronRuby/F#  ORM

留言反馈

#回复: 使用Jasper和IronPython操作数据 编辑
在微软的Web 2.0大会上,官员们开始介绍“Jasper”。在一场名为“用Dynamic ADO.Net快速建立数据驱动网页”的活动中,微软的官员们开始介绍如何同时使用动态语言的概念和ADO.Net来开发


2008-02-01 18:50:00 | [匿名用户:手机窃听器]
#回复: 使用Jasper和IronPython操作数据 编辑
非常实用
2007-09-27 08:06:00 | [匿名用户:锅炉]
#回复: 使用Jasper和IronPython操作数据 编辑
非常实用,代码也精典。
2007-09-27 08:06:00 | [匿名用户:锅炉]
#回复: 使用Jasper和IronPython操作数据 编辑
不错.
2007-06-24 10:38:00 | [匿名用户:窃听器]
#动态语言满足动态数据库开发 编辑
在微软的Web 2.0大会上,官员们开始介绍“Jasper”。在一场名为“用Dynamic ADO.Net快速建立数据驱动网页”的活动中,微软的官员们开始介绍如何同时使用动态语言的概念和ADO.Net来开发
2007-05-20 19:02:00 | [匿名用户:毛遂自荐博客集]
#动态语言满足动态数据库开发 编辑
2007-05-20 18:56:00 | [匿名用户:自由、创新、研究、探索……]
#回复: 使用Jasper和IronPython操作数据 编辑
语言层面的语法糖对于ORM和DAL没有颠覆性的影响,起码来说对我是这样的,DLINQ让SQL的查询构造编写更加简洁, 虽然在C#2.0上通过范型也能模拟出强类型查询, 但代码要臃肿得多, 如果以后有IDE的强力支持,加上method missing的语法,那才是真正简化和加快我们的开发。
2007-05-04 21:50:00 | [匿名用户:窃听器]
#回复: 使用Jasper和IronPython操作数据 编辑
我不知道这是不是一种倒退

一开始看到内存中生成实体类,感觉好帅,后来看到原来是使用延迟绑定 感觉和ruby的method missing差不多 在我看来
ctx.Posts和p.getEntity("Posts")的区别只在于书写的美观了 最希望的是ide能够支持到直接获取Schema,然后对于这种动态属性也能给出智能提示,在编译前IDE也能有个预处理,通过指定connectstring来检查上面的动态属性和方法是否能和数据库的Schema一致

语言层面的语法糖对于ORM和DAL没有颠覆性的影响,起码来说对我是这样的,DLINQ让SQL的查询构造编写更加简洁, 虽然在C#2.0上通过范型也能模拟出强类型查询, 但代码要臃肿得多, 如果以后有IDE的强力支持,加上method missing的语法,那才是真正简化和加快我们的开发
2007-05-03 11:29:00 | [匿名用户:progame]
#回复: 使用Jasper和IronPython操作数据 编辑
对. 这是Jasper利用了实体框架的功能
2007-05-02 10:01:00 | [匿名用户:saucer]
#回复: 使用Jasper和IronPython操作数据 编辑
>>> p.User = u
请问这个User是哪来的呢?
Jasper看到FOREIGN KEY (UserID) References Users(UserID)生成的?
2007-05-02 09:42:00 | [匿名用户:Some Cai BirD]
#回复: 使用Jasper和IronPython操作数据 编辑
真好,思归,你去参加Mix 07了吗
2007-05-02 09:05:00 | [匿名用户:shanyou]
#回复: 使用Jasper和IronPython操作数据 编辑
真好
2007-05-02 09:04:00 | [匿名用户:shanyou]
博客主人设置本博客不允许匿名用户发表言论,请登录后再试

Powered by: Joycode.MVC引擎 0.5.1.8