3
这是数据模型我有:MINLENGTH到上ICollection的约束失败,实体框架
public class Team
{
[Key]
public int Id { get; set;}
[Required]
public string Name { get; set; }
[MinLength(1)]
public virtual ICollection<User> Users { get; set; }
}
我的问题是,当我后来尝试创建一个新的团队(有一个用户),我得到了以下问题时,上下文正在保存。
在调用System.ComponentModel.DataAnnotations.MinLengthAttribute.IsValid时验证'用户'期间引发了意外的异常。详情请参阅内部例外。
内部异常如下:
{“无法转换类型的对象 'System.Collections.Generic.List`1 [MyNameSpace.Model.User]' 为类型“系统。 。阵”“}
下面是实际节电的代码(现在是在控制器):
if (ModelState.IsValid)
{
team.Users = new List<User>();
team.Users.Add(CurrentUser);//CurrentUser is a property that gives me the currently active User (MyNamespace.Model.User).
DB.Teams.Add(team);//DB is a DbContext object that holds DbSets of all my models
DB.SaveChanges();
return RedirectToAction("Index");
}
那么,是什么在这里呢?我做错了什么,还是有其他事情发生?
你的回答似乎是正确的,但是你错误地引用了MSDN页面。它显示为:“指定属性中允许的数组或字符串数据的最小长度。” (或不是)。 不幸的是,他们选择不支持收藏。这似乎是明显的选择,但是哦... – 2017-05-05 16:38:45