我开始在我的WPF应用程序中使用ValidationRules,但很困惑.
我有以下简单的规则:
class RequiredRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (String.IsNullOrWhiteSpace(value as string))
{
return new ValidationResult(false, "Must not be empty");
}
else
{
return new ValidationResult(true, null);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在XAML中使用如下:
<TextBox>
<TextBox.Text>
<Binding Path="Identity.Name">
<Binding.ValidationRules>
<validation:RequiredRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
这大多数工作正如我所料.我很惊讶地看到我的源属性(Identity.Name)没有被设置; 我有一个永远不会看到变化的撤销功能,除了重新输入它之外没有办法恢复该值(不好).
Microsoft的数据绑定概述描述了底部附近的验证过程,这很好地解释了这种行为.基于此,我想让我的ValidationStep设置UpdatedValue.
<validation:RequiredRule ValidationStep="UpdatedValue"/>
Run Code Online (Sandbox Code Playgroud)
这就是我的事情变得奇怪的地方.而不是使用对象值调用Validate()作为设置的属性值(即字符串),我得到了System.Windows.Data.BindingExpression!我没有在Microsoft的文档中看到任何描述此行为的内容.
在调试器,我可以看到源对象(DataContext的TextBox),导航路径的特性,并看到该值已被设定.但是,我没有看到在验证规则中找到正确属性的任何好方法.
注意:使用ValidationStepas ConvertedProposedValue,我得到输入的字符串(我没有使用转换器),但它也会在验证失败时阻止源属性更新,如预期的那样.有了CommittedValue,我得到了BindingExpression而不是字符串. …
我基于现有数据库创建了一个实体框架模型,然后从模型中生成了POCO实体.我的web.config中的连接字符串不是实体框架,它只是标准连接字符串(它缺少CSDL,SSDL,MSL引用).
我可以编译我的应用程序,但是当我运行时,我收到此错误:
如果在Code First模式下使用,使用T4模板为Database First和Model First开发生成的代码可能无法正常工作.要继续使用Database First或Model First,请确保在执行应用程序的配置文件中指定了Entity Framework连接字符串.要使用从Database First或Model First生成的这些类,使用Code First添加任何其他配置,使用属性或DbModelBuilder API,然后删除引发此异常的代码
我的问题是,在我的代码中,它实现了POCO来自自动生成,我怎样才能让它像Code First一样?我不想在我的连接字符串中引用CSDL等.
c# entity-framework code-first entity-framework-4.1 database-first