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