Entity framework 4 实体框架4 w/MVC 2-对抗EF4和x27;s无法在表单字段中允许空字符串
最终编辑:成功!有一个数据注释规则可以阻止空字符串被设置为null。我可以在这里使用解决方案()。现在很有魅力。我将保留我的帖子的其余部分,以防有人能从中吸取教训 决定重写这篇文章,尽我所能提供所有细节。长长的帖子,给出了所有的代码,所以请容忍我 我正在以EF4为基础编写一个MVC2项目。我的所有db列都不可为空 正如我之前所说,当我测试空表单的情况时,EF4抛出ConstraintException时,我遇到了一些问题。异常是从Designer.cs文件中出现的,特别是在以下代码中:Entity framework 4 实体框架4 w/MVC 2-对抗EF4和x27;s无法在表单字段中允许空字符串,entity-framework-4,Entity Framework 4,最终编辑:成功!有一个数据注释规则可以阻止空字符串被设置为null。我可以在这里使用解决方案()。现在很有魅力。我将保留我的帖子的其余部分,以防有人能从中吸取教训 决定重写这篇文章,尽我所能提供所有细节。长长的帖子,给出了所有的代码,所以请容忍我 我正在以EF4为基础编写一个MVC2项目。我的所有db列都不可为空 正如我之前所说,当我测试空表单的情况时,EF4抛出ConstraintException时,我遇到了一些问题。异常是从Designer.cs文件中出现的,特别是在以下代码中:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String GameTitle
{
get
{
return _GameTitle;
}
set
{
OnGameTitleChanging(value);
ReportPropertyChanging("GameTitle");
_GameTitle = StructuralObject.SetValidValue(value, false); // <-- this is where the exception is being thrown
ReportPropertyChanged("GameTitle");
OnGameTitleChanged();
}
}
private global::System.String _GameTitle;
partial void OnGameTitleChanging(global::System.String value);
partial void OnGameTitleChanged();
}
同样,它没有起作用
正在尝试简单的数据批注:
[MetadataType(typeof(GameValidation))]
public partial class Game
{
class GameValidation
{
[Required(ErrorMessage="A game must have a title")]
public string GameTitle { get; set; }
[Required(ErrorMessage = "A game must have an associated review")]
public string ReviewText { get; set; }
[Required(ErrorMessage="A game must have a list of pros associated with it")]
public string Pros { get; set; }
[Required(ErrorMessage="A game must have a set of cons associated with it")]
public string Cons { get; set; }
}
}
也不管用
所有这些的共同点是EF设计器抛出异常,而该异常未被MVC捕获。我完全不知所措
编辑:从此处交叉发布:
其他人也有同样的问题:就我个人而言,我不认为将此存储在数据库中有什么意义:
Id Description
1 Foo
2 ''
3 Bar
我觉得这很傻
不存储空白字段,而是使其可为空
如果不同意,则可以在数据库中设置默认值,并将StoreGeneratedPattern设置为Computed
或者您可以为实体设置一个ctor,用于设置值 空白字段是指web表单字段,而不是db字段。在我的例子中,db列不应该为null,因为它在逻辑上没有意义说明它们对应的行应该表示什么。但是,如果用户输入一个空白字段并提交表单,您希望在数据库中存储什么?这听起来更像是UI问题而不是EF问题。啊,我明白你的意思了。不,我也不想存储空白表单字段。我只想在我自己的验证代码中处理这个错误条件,而不是在我做任何事情之前EF4抛出ConstraintException。好的,明白了-所以你想抛出一个验证错误。你用的是什么用户界面?MVC?网络表单?银灯?WPF?听起来像是其中一个抛出验证错误的工作,通常是通过模型属性上的数据注释来完成的。它甚至不应该使用MVC到达EF。是的,这就是我所想的,但是看起来MVC的自动模型绑定导致了这个问题。由于它采用表单字段值并试图创建一个新的模型对象,其中填充了这些值,所以它的验证是在异常到达我的控制器代码之前触发并抛出异常。
public class RuleViolation
{
public LambdaExpression Property { get; set; }
public string Message { get; set; }
}
public class RulesException : Exception
{
public readonly IList<RuleViolation> Errors = new List<RuleViolation>();
private readonly static Expression<Func<object, object>> thisObject = x => x;
public void ErrorForModel(string message)
{
Errors.Add(new RuleViolation { Property = thisObject, Message = message });
}
}
public class RulesException<TModel> : RulesException
{
public void ErrorFor<TProperty>(Expression<Func<TModel, TProperty>> property, string message)
{
Errors.Add(new RuleViolation { Property = property, Message = message });
}
}
public partial class Game : IDataErrorInfo
{
public string Error
{
get
{
if (Platforms.Count == 0)
{
return "A game must be associated with at least one platform";
}
return null;
}
}
public string this[string columnName]
{
get
{
switch (columnName)
{
case "GameTitle":
if (string.IsNullOrEmpty(GameTitle))
{
return "Game must have a title";
}
break;
case "ReviewText":
if (string.IsNullOrEmpty(ReviewText))
{
return "Game must have an associated review";
}
break;
case "Pros":
if (string.IsNullOrEmpty(Pros))
{
return "Game must have a list of pros";
}
break;
case "Cons":
if (string.IsNullOrEmpty(Cons))
{
return "Game must have a list of cons";
}
break;
}
return null;
}
}
}
[MetadataType(typeof(GameValidation))]
public partial class Game
{
class GameValidation
{
[Required(ErrorMessage="A game must have a title")]
public string GameTitle { get; set; }
[Required(ErrorMessage = "A game must have an associated review")]
public string ReviewText { get; set; }
[Required(ErrorMessage="A game must have a list of pros associated with it")]
public string Pros { get; set; }
[Required(ErrorMessage="A game must have a set of cons associated with it")]
public string Cons { get; set; }
}
}
Id Description
1 Foo
2 ''
3 Bar