【发布时间】:2014-02-03 08:31:12
【问题描述】:
我是 LINQ 的新手,使用 Linq to Sql 在 asp.net 中工作,我正在尝试验证数据模型字段。我的应用程序“Common”和“Administration”中有两个项目(类库)意味着两个不同的命名空间,我的应用程序是基于层的“数据访问层”和“业务流程层”
我在 Generated Class 中使用 OnValidate 方法来验证字段
命名空间通用
[Table(Name="dbo.tblaccounts")]
public partial class tblaccount : INotifyPropertyChanging, INotifyPropertyChanged
{
#region Extensibility Method Definitions
partial void OnValidate(System.Data.Linq.ChangeAction action)
{
if (!Char.IsUpper(this._ACCOUNT[0]))
{
throw new ValidationException(
"Garage Name must start with an uppercase letter.");
}
}
}
数据访问层中的命名空间管理
internal bool SaveGarage(tblaccount oGarage)
{
ChangeSet changeSet = null;
int changeCount = 0;
using (CommonDataContext GarageDC = new CommonDataContext(Settings.ConnectionString))
{
GarageDC.DeferredLoadingEnabled = false;
try {
if (oGarage.RowVersion == null)
{
//insert Garage details
GarageDC.tblaccounts.InsertOnSubmit(oGarage);
changeSet = GarageDC.GetChangeSet();
changeCount = changeSet.Inserts.Count;
GarageDC.SubmitChanges();
Message = "Garage saved successfully.";
}
else
{
//updates a Garage
GarageDC.tblaccounts.Attach(oGarage, true);
changeSet = GarageDC.GetChangeSet();
changeCount = changeSet.Updates.Count;
try
{
GarageDC.SubmitChanges();
Message = "Garage Updated successfully.";
}
catch (ChangeConflictException cex)
{
foreach (ObjectChangeConflict conflictObject in GarageDC.ChangeConflicts)
{
// expose the neccessary information,
}
}
}
}
// here i want to get the validation exception
catch (Exception ex)
{
Message = ex.ToString();
}
}
if (changeCount > 0) { return true;
}
else { return false; }
}
如果您在应用程序的不同项目中使用不同的命名空间,请建议我如何验证数据模型字段...我已经在管理项目中引用了一个公共命名空间 dll
我在构建项目时遇到此错误
没有找到用于实现部分声明的定义声明 方法 'PeaceStar.Common.Data.tblaccount.OnValidate(System.Data.Linq.ChangeAction)'
【问题讨论】:
-
你为什么将你的方法 OnValidate 定义为“部分”?
标签: c# asp.net asp.net-mvc linq