- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Users
和 Notifications
之间创建一种关系,我认为这是一种一对多关系。
我在这方面遇到了麻烦,并且不确定我是怎么出错的。这是两个类的代码:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public virtual ICollection<Notification> Notifications { get; set; }
}
public class Notification
{
public int NotificationID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime Time { get; set; }
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
我还有以下映射:
modelBuilder.Entity<Notification>()
.HasRequired<ApplicationUser>(u => u.ApplicationUser)
.WithMany(u => u.Notifications)
.HasForeignKey(u => u.NotificationID);
我在尝试修复此问题时遇到了不同的错误,例如:
编辑:
当我尝试为所有用户添加通知时,会抛出相同的异常(违反了多重性约束。关系Notification_ApplicationUser'的角色'Notification_ApplicationUser_Target'具有多重性1或0..1。) :
public void AddNotification(Notification n)
{
var roleId = context.Roles.Where(m => m.Name == "User").Select(m => m.Id).SingleOrDefault();
var users = context.Users.Where(u => u.Roles.All(r => r.RoleId == roleId)).ToList();
try
{
foreach(var user in users)
{
user.Notifications.Add(n);
}
context.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
最佳答案
我认为问题出在外键的字符串类型上。默认情况下,字符串在数据库级别可为空,但所需的外键不能为空。指定 HasRequired
仅强制要求该关系是必需的,而不强制外键属性必须不为空。我认为如果您只需将 [Required]
添加到外键属性中,就可以解决问题:
[Required]
public string ApplicationUserID { get; set; }
显然,您需要进行迁移才能将此更改应用到您的数据库。
编辑
抱歉,我读错了异常。问题的根源实际上是向每个用户添加相同的通知,就像字面上相同的通知一样。 EF 进行对象跟踪,因此在添加一次 n
引用的实体实例后,EF 就会对其进行跟踪。然后,当您尝试将其添加到另一个用户时,它会认为您正在尝试执行多对多操作,基本上,单个 Notification
记录将属于多个用户。
在我的脑海中,我不确定在 EF 中解决这种困惑的最佳方法是什么,但我有一些潜在的想法:
您可以在添加实体后尝试分离该实体:
user.Notifications.Add(n);
context.Entry(n).State = EntityState.Detached;
但是,我不确定这是否允许添加它。你必须进行测试。如果没有创建它,您也可以尝试在分离之前保存。我认为这可行,但每次迭代都提交到数据库显然效率很低。
最安全的方法是为每个实例创建一个新实例,然后简单地映射 n
中的数据:
var notification = new Notification
{
Title = n.Title,
Description = n.Description,
Time = n.Time
};
user.Notifications.Add(notification);
这将确保您添加的每个通知都是完全独立的跟踪实例。
关于asp.net-mvc - Code First 中的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43076682/
目前,我正在为网络开发类(class)做作业。 这些是说明:第一行和首字下沉样式Jakob 希望文章的第一行以小写大写字母显示。转到 First Line and Drop Cap Styles 部分
.first() 方法是在 jQuery 1.4 中添加的。 :first 选择器自 1.0 以来就已存在。 来自文档: :first The :first pseudo-class is equiv
我正在审查现有的 ASP.NET MVC (5.2.3) EF (6.1.3) 项目。 该项目使用 ASP.NET Identity,我检查了 web.config 中的 2 个连接字符串,一个用于
为什么人们使用 mid=first+(last-first)/2 而不是 (first+last)/2,在二进制搜索的情况下)两者有区别吗。如果有,请告诉我,因为我无法理解其中的区别。 最佳答案 如果
为什么人们使用 mid=first+(last-first)/2 而不是 (first+last)/2,在二进制搜索的情况下)两者有区别吗。如果有,请告诉我,因为我无法理解其中的区别。 最佳答案 如果
for(auto it = M.begin(); it!=M.end();it++) { coutfirstsecondsecond == 1) return it->firs
我试图从第二个循环中获取循环的第一项。 我知道我得到了这样的@key @../key 但@first 似乎不像@../first 那样工作 有什么想法吗? 问候 最佳答案 首先,无论是否在嵌套 blo
var tab1 = $('.tabs a:first-child').attr('href'); alert(tab1); .. 尽管同一页面上有两个 div.switch,但仅匹配一个。第二个位于
我想知道如何将节点*变量 NODE 分配给结构内的数据? struct node { int info; struct node *link; }; typedef struct nod
我有两个段落包含在一个 div 中。我想让第一段的文字变大一点,但使用 :first-child 并不能像我所说的那样工作。看不出有什么问题。
我有一个 ul li 列表 Parent child1 child2
我有三个表,即员工、部门和申诉。 Employees 表有超过一百万条记录。我需要找到员工的详细信息、他/她的部门以及他/她提出的申诉。 我可以想到以下两个查询来查找结果: 1。先过滤记录,只获取需要
我有三个表,即员工、部门和申诉。 Employees 表有超过一百万条记录。我需要找到员工的详细信息、他/她的部门以及他/她提出的申诉。 我可以想到以下两个查询来查找结果: 1。先过滤记录,只获取需要
这有什么区别吗: myList.Where(item => item == 0).First(); 还有这个: myList.First(item => item == 0); 后者对我来说更有意义,
我分不清 element:first-child 之间的区别和 element:first-of-type 例如,你有一个 div div:first-child → 全部 元素是其父元素的第一个子元
当我遇到一个奇怪的情况时,我正在研究 CSS 选择器。 如果我使用 :first-child 伪元素,我需要在它前面加上一个空格才能工作,否则它将无法工作。然而 :first-letter 伪元素的情
请考虑以下字符串数组: let strings = ["str1", "str2", "str10", "str20"] 假设需要获取包含 5 个字符的第一个元素 (String),我可以使用 fil
让我们假设我们要开始新项目 - 包含一些业务逻辑的应用程序、ASP.NET 上的用户界面、WPF 或两者。我们想使用 ORM 或 DAL 代码生成器并在 .NET 类中实现我们的业务逻辑。我们可以通过
我有一种树系统。我想做的是给所有 parent 一个 margin ,除了第一个。这是我的 HTML: Test
我分不清 element:first-child 之间的区别和 element:first-of-type 例如,你有一个 div div:first-child → 全部 元素是其父元素的第一个子元
我是一名优秀的程序员,十分优秀!