未解决的问题.... ActiveRecordValidationBase不会在Save(同时更新)上抛出ActiveRecordValidationException ... 但是,调用Create throws ActiveRecordValidationException ...我真的不明白为什么......下面的示例代码。
[Web.Config中]
<castle>
<facilities>
<facility
id="ActiveRecordFacility"
type="Castle.Facilities.ActiveRecordIntegration.ActiveRecordFacility, Castle.Facilities.ActiveRecordIntegration"
isDebug="true"
isWeb="true">
<assemblies>
<item>Base.Models</item>
</assemblies>
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
<add key="connection.connection_string" value="ConnectionString = ${connstr}"/>
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="hbm2ddl.auto" value="update"/>
<add key="connection.release_mode" value="on_close" />
<add key="use_outer_join" value="true"/>
<add key="use_proxy_validator" value="true"/>
<add key="hbm2ddl.keywords" value="auto-quote"/>
<add key="use_reflection_optimizer" value="true"/>
<add key="bytecode.provider" value="lcg"/>
<add key="max_fetch_depth" value="2"/>
<add key="show_sql" value="false"/>
</config>
</facility>
</facilities>
</castle>
<system.web>
<httpModules>
<add name="SessionScopeWebModule" type="Castle.ActiveRecord.Framework.SessionScopeWebModule, Castle.ActiveRecord.Web"/>
</httpModules>
</system.web>
[模型(权限 - 仅属性和验证)]
#region Public Virtual Properties
[PrimaryKey("permission_id", Generator = PrimaryKeyType.GuidComb)]
public virtual Guid Id { get; set; }
[ActiveRecordProperty("area", Length = 256, SqlType = "NVARCHAR(256)", UniqueKey = "UQ_PERMISSION")]
[ValidateRegExp(@"^\D\w+$", ErrorMessageKey = "Permission_Area_Regexp", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
public virtual string Area { get; set; }
[ActiveRecordProperty("controller", Length = 256, SqlType = "NVARCHAR(256)", UniqueKey = "UQ_PERMISSION")]
[ValidateNonEmpty(ErrorMessageKey = "Permission_Controller_NonEmpty", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
[ValidateRegExp(@"^\D\w+$", ErrorMessageKey = "Permission_Controller_Regexp", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
public virtual string Controller { get; set; }
[ActiveRecordProperty("action", Length = 256, SqlType = "NVARCHAR(256)", UniqueKey = "UQ_PERMISSION")]
[ValidateNonEmpty(ErrorMessageKey = "Permission_Action_NonEmpty", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
[ValidateRegExp(@"^\D\w+$", ErrorMessageKey = "Permission_Action_Regexp", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
public virtual string Action { get; set; }
[ActiveRecordProperty("date_created")]
public virtual DateTime DateCreated { get; set; }
[ActiveRecordProperty("date_modified")]
public virtual DateTime DateModified { get; set; }
[HasAndBelongsToMany(Cascade = ManyRelationCascadeEnum.All, ColumnKey = "permission_id", ColumnRef = "role_id", Lazy = true, MapType = typeof(Role), Table = "permissions_in_roles")]
[ValidateCollectionNotEmpty]
public virtual IList<Role> Roles { get; set; }
#endregion
[控制器 - 保存操作]
[HttpPost]
public ActionResult Save(Permission permission)
{
try
{
if (permission.Id == Guid.Empty)
permission.Create();
else
permission.Update();
// permission.Save();
// Call Update or Save, with invalid model and does not throw invalid exception (???)
// Create seems ok.
}
catch (Exception ex)
{
TempData["permission"] = permission;
TempData["exception"] = ex;
if (permission.Id == Guid.Empty)
return RedirectToAction("Create");
return RedirectToAction("Edit", new { id = permission.Id });
}
return RedirectToAction("List", new { scope = SearchScope.Restricted, id = permission.Id });
}
[ModelBinder的]
public sealed class PermissionModelBinder : IModelBinder
{
#region IModelBinder Members
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
var model = Permission.TryFind(request.Get<Guid>("permission.Id")) ?? new Permission();
var now = DateTime.Now;
model.Area = request.Get<string>("permission.Area");
model.Controller = request.Get<string>("permission.Controller");
model.Action = request.Get<string>("permission.Action");
if (model.DateCreated == DateTime.MinValue)
model.DateCreated = now;
model.DateModified = now;
return model;
}
#endregion
}
使用MS Sql2005,ActiveRecord 3,NHibernate 3,ASP.NET MVC 2。 有没有人遇到与我打交道相似的问题?管理解决?
编辑:我意识到只有在实体更新时,ActiveRecordValidationBase类才会调用BeforeSave(IDictionary状态)方法,但是在创建实体时,类会调用它。由于此方法检查实体是否有效。这是一个错误还是一些错误的配置? (使用ActiveRecord源和NHibernate进行调试)谢谢!
中添加了示例