我最近使用Entity Framework 5开发了一个.net 4.5库.一切正常,直到我被告知我必须降级到.net 4.0才能与Windows 2003保持兼容.
我使用程序包管理器控制台卸载了EF 5并获得了成功的消息,然后这个消息:
Failed to generate binding redirects for 'MyProjectName'. An item with the same key has already been added.
Run Code Online (Sandbox Code Playgroud)
降级和安装Entity Framework后会出现相同的消息.
一切都在编译.但是在执行时我有这个例外:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
Could not load file or assembly 'EntityFramework, Version=5.0.0.0,Culture=neutral,
PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's
manifest definition does not match the assembly reference. (Exception from HRESULT:
0x80131040)":"EntityFramework, Version=5.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
Run Code Online (Sandbox Code Playgroud)
在Web.Config(以及库的App.Config)中,我有以下几行:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
...
<runtime>
<assemblyBinding …Run Code Online (Sandbox Code Playgroud) 当我实现从ValidationAttribute类继承的自定义属性时,我总是会覆盖bool IsValid(object value)方法,而不是将其原型为的另一个方法ValidationResult IsValid(objet value, ValidationContext validationContext)。
也许,即使我不使用验证上下文或结果(我对EntityFramework和ModelState.IsValidcontroller属性使用验证),也应该改写第二种方法。或继续忽略重载方法。如果是这样,根据调用属性验证的上下文,我可以使一个对象有效还是无效?如下面的代码所示,这种情况是否有问题?
class StrictlyPreviousAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
var dateTime = value as DateTime?;
return dateTime == null || dateTime <= DateTime.Today;
}
}
class PreviousAttribute : StrictlyPreviousAttribute
{
public override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var dateTime = value as DateTime?;
if(dateTime == DateTime.Today)
{
return ValidationResult.Success;
}
else
{
return base.IsValid(object);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道是否存在对象误解,或者我是否缺少某些要点。有没有一种我应该优先替代的方法。我应该同时覆盖两者。