Chapter Data Modification & Chapter Data Validation
Chapter Data Modification
XF的数据提交,支持单行、集合和多层次的master-details结构的数据。
Create
当提交如下数据
<Job>
<Id/>
<Name>CEO</Name>
<Allowance>1000</Allowance>
<Descr/>
</Job>
Create方法在数据库表Jobs中插入一条返回数据。注意,提交的数据中没有提供Id的值,
表示Id字段为自增长或取自序列Sequence值(在schema Field <Id>指定)。
也可以在提交数据中设置Id的值,显然,对于自增长字段,设置了也无效,但会优先于序列Sequence,也就是,设置了Id的值,就不会去序列取值了。
多层master-details举例:
<Job>
<Id/>
<Name>Clerk</Name>
<Allowance>0</Allowance>
<Descr/>
<Employees>
<Employee>
<Id/>
<Name>John Smith</Name>
<Users>
<User>
<Id/>
<UserName>John</UserName>
</User>
</Users>
</Employee>
<Employee>
<Id/>
<Name>Michael Gill</Name>
<Users>
<User>
<Id/>
<UserName>Gill</UserName>
</User>
</Users>
</Employee>
</Employees>
</Job>
同样,方法结束,所有的Id值都会填入。
多对多:
<User>
<Id/>
<UserName>Carl</UserName>
<Roles>
<Role>
<Id>1</Id>
...
</Role>
<Role>
<Id>2</Id>
...
</Role>
</Roles>
</User>
同样符合多对多“短路”规则,如果<Role>再内嵌一个集合(Set),将被丢弃。
提交数据如果存在计算字段或外表字段(如:<Job.Name>CTO</Job.Name>),将被丢弃。
Delete
XF在删除master-details结构的details时,与EF一样,会根据schema中detail的关系字段(外键字段)是否为空,来决定是删除行,还是设置关系字段为空。
例如:
<Job>
...
<Employees>
<Employee>
...
</Employee>
...
</Employees>
</Job>
如果Employees表中字段JobId允许为空,不是Delete Employees…,而是Update Set JobId = NULL…,因此,Delete方法会触发Updating事件。
Update
在Update master-details结构的数据时,相当于“Put”。
如通过并发检查,Update会强制同步数据库相应的数据行为提交数据,也就是说对于details,会和数据库相应数据进行比较,
如果数据库中不存在就Insert,反之就是Update,数据库存在而提交数据中不存在的则删除。因此,不但会触发Updating事件,也可能触发Inserting、Inserted和Deleting事件。
UpdateUpdateWithOriginal
考虑一个典型的Update场景,首先获取需要编辑的(层次master-details)原始数据,然后(一般有个UI)进行修改,
最后把修改完的数据提交保存(上述Update方法)。如果,不光提交修改后的数据还把开始获取的原始数据一并提交,
就需要用到UpdateUpdateWithOriginal,这里的Original就是指原始数据。
与Update方法类似,不同的是UpdateUpdateWithOriginal比较的数据不再是数据库中的数据,而是Original。
事务包
所谓事务包就是把一系列方法调用打包在一个<Transaction>里面,这一系列方法在同一事务内执行。
XF用ElementContext.SaveChanges(XElement packet)方法保存事务包。
事务包举例如下:
<Transaction>
<Create Schema="Membership">
<Users>
<User>
...
</User>
</Users>
</Create>
<Delete>
<Role>
...
</Role>
</Delete>
<Update Schema="Membership" Config="Staff">
<Config Name="Staff" Version="1.2.3">
...
</Config>
<Employee>
...
</Employee>
<Employee Original="True">
...
</Employee>
</Update>
</Transaction>
<Create Schema="Membership"> 指定使用名为Membership的NamedSchema
<Update Schema="Membership" Config="Staff"> 指定使用名为Membership的NamedSchema,并且内嵌一个Name为Staff的modifying schema。
<Employee Original="True"> 指出调用的是UpdateWithOriginal。调用Update方法,不需要<Employee Original="True">。
事务
上述方法都会自己管理事务。如果业务足够复杂,要编写代码启动事务,需按下列示意代码编写:
ElementContext elementContext = ...;
elementContext.Database.Connection.Open();
elementContext.Database.Transaction = elementContext.Database.Connection.BeginTransaction();
try
{
...
elementContext.Database.Transaction.Commit();
}
catch
{
elementContext.Database.Transaction.Rollback();
throw;
}
finally
{
elementContext.Database.Connection.Close();
elementContext.Database.Transaction = null;
}
并发
XF与EF一样,通过下面2个Attribute来控制(数据库行)并发:
TimestampAttribute
ConcurrencyCheckAttribute
在Delete或Update每个数据库行时,会检查上述2个Attribute标记的字段的值和数据库当前值是否一致,如不一致,则抛出ConcurrencyCheckException异常。
事件
ElementContext有四个事件:
Inserting 在插入数据行时发生,
事件参数的Node属性,就是将要插入数据库的行数据。这时还未插入数据库,是最后修改Node的机会。下面~ing事件同。
Inserted 在插入数据行后发生
事件参数的Node属性,是插入数据库后的行数据。这时自增长或取自序列的Id,已经回插到Node中。
事件参数的After属性,允许添加一组Sql,在插入后执行。相当数据库的After行触发器。
Deleting 在删除数据行时发生
事件参数的Before属性,相当数据库的After行触发器。
Updating 在更改数据行时发生
事件参数有After、Before属性。
Chapter Data Validation
在每个Element提交数据库前,都会对其验证,验证失败时,抛出ElementValidationException异常(内部封装ElementValidationResult[])。
当然,也可以调用ElementContext.GetValidationResults(…)来返回ElementValidationResult[],不提交数据库而直接验证。
XF验证完全仿照EF,支持下列Attribute,均继承自System.ComponentModel.DataAnnotations.ValidationAttribute:
CustomValidationAttribute
DataTypeAttribute
RangeAttribute
RegularExpressionAttribute
RequiredAttribute
StringLengthAttribute
MaxLengthAttribute
MinLengthAttribute
CreditCardAttribute
EmailAddressAttribute
PhoneAttribute
UrlAttribute
也可能会用到(如果配置的话):
DisplayNameAttribute
DisplayAttribute
在EF中,典型的类定义:
public class User
{
[Key]
public int Id { get; set; }
[DisplayName("用户名")]
[Required(AllowEmptyStrings = true)]
[MinLength(6)]
public string UserName { get; set; }
[DisplayName("密码")]
[Required(AllowEmptyStrings = true)]
[MinLength(6)]
public string Password { get; set; }
...
}
XF相应的配置(在schema中)如下:
<User Set="Users">
<Id>
<key/>
</Id>
<UserName>
<DisplayName>
<DisplayName>用户名</DisplayName>
</DisplayName>
<Required>
<AllowEmptyStrings>True</AllowEmptyStrings>
</Required>
<MinLength>
<Length>4</Length>
</MinLength>
</UserName>
<Password>
<DisplayName>
<DisplayName>密码</DisplayName>
</DisplayName>
<Required>
<AllowEmptyStrings>True</AllowEmptyStrings>
</Required>
<MinLength>
<Length>6</Length>
</MinLength>
</Password>
…
</User>
注:SQL Server 需设置<AllowEmptyStrings>True</AllowEmptyStrings>
除此之外,事件ElementContext.Validating 也会在验证Element时发生。
ValidationAttribute不足以应付的复杂或特殊的业务规则可以在此通过写代码来验证,
在事件参数ValidatingEventArgs中的ValidationResults添加未通过验证的ValidationResult。
Chapter Data Modification & Chapter Data Validation的更多相关文章
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...
- 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。
我用的是 spring + springmvc + mybatis +mysql. <tx:advice id="txAdvice" transaction-manager= ...
- Connection is read-only. Queries leading to data modification are not allowed
看了下mysql-connector-5.1.40版本中,如果设置failoverReadOnly=true (即默认值,参考链接),当mysql连接failover时,会根据jdbc连接串将当前连接 ...
- java最全的Connection is read-only. Queries leading to data modification are not allowed
Connection is read-only. Queries leading to data modification are not allowed 描述:框架注入时候,配置了事物管理,权限设置 ...
- [Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed ...
- Datasets for Data Mining and Data Science
https://github.com/mattbane/RecommenderSystem http://grouplens.org/datasets/movielens/ KDDCUP-2012官网 ...
- 【转】浏览器中的data类型的Url格式,data:image/png,data:image/jpeg!
所谓"data"类型的Url格式,是在RFC2397中 提出的,目的对于一些"小"的数据,可以在网页中直接嵌入,而不是从外部文件载入.例如对于img这个Tag, ...
- SQL data reader reading data performance test
/*Author: Jiangong SUN*/ As I've manipulated a lot of data using SQL data reader in recent project. ...
- 初探 spring data(一)--- spring data 概述
由于自己一个项目要用多到Sql与NoSql两种截然不同的数据结构,但在编程上我希望统一接口API,让不同类型的数据库能在相同的编程接口模式下运作.于是找了一个spring的官网,发现一个spring ...
随机推荐
- java通过ftp和sftp上传war包上传到Linux服务器实现自动重启tomcat的脚本代码
ar包自动上传Linux并且自动重启tomcat 用的是jdk1.7出的文件监控 支持ftp和sftp,支持多服务器负载等 配置好config 非maven项目导入直接使用 #\u76D1\u542C ...
- 删除文件夹工具类 DeleteFolder.java
package com.util; import java.io.File; /** * 删除文件夹 * @createTime DSC 20, 2010 15:38 * @version 2.0 * ...
- ORA-01109:数据库未打开
ORA-01109:数据库未打开 在此之前做了这样一操作,在plsql创建了2表空间,由于装的是oracle精简版所以创建表空间大小超过4G就不能创建,然后我就手动把表空间给删除了,回收站也给删了,问 ...
- java单例模式和双例模式
今天朋友找我给做道题,双例模式,我是没听说过,都说是单例模式和多例模式, 也不知道双例模式什么时候用,就简单写了一个案例,不知道对不对,个人感觉蛮对的,双例就是单例+单例,废话不说了!!!! /* * ...
- cmd命令运行php,php通过cmd运行文件
一.cmd命令运行php 1.通过配置环境变量 >php "php文件" 如果要待参数 php -q "php文件" 参数 php获取参数 $a = $a ...
- 在WINDOWS SERVER 上或远程桌面中使用 MUTEX
引用: http://www.cnblogs.com/fg0711/archive/2012/05/03/2480502.html 使用Mutex需要注意的两个细节 可能你已经注意到了,例子中在给Mu ...
- Nexus手动更新索引
如果有耐心的话,完全可以通过在线更新索引的方式来做,但所消耗的时间较长,下面介绍一种简单.可行的方式来手动更新索引文件. 访问http://repo.maven.apache.org/maven2/. ...
- Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value 解决处理
首先应该明白报这个错误说明了你用的jedis方法与redis服务器中存储数据的类型存在冲突. 例如:数据库中有一个key的数据存储的是Hash类型的,但是你使用jedis执行数据操作的时候却使用了非H ...
- erlang的escript脚本
参考霸爷的博客 测试例子 #!/usr/bin/env escript %%! -smp enable -sname mmcshadow -mnesia debug verbose[/color] m ...
- 【MySQL】binlog缓存的问题和性能
之前在没有备库的情况下,遇到过more than 'max_binlog_cache_size' bytes of storage 的错误,今天在主备复制的时候又遇到了这个问题 Last_SQL_Er ...