- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的问题原来是有两个上下文。我稍微修改了我的代码,使其只有一个上下文,然后我的问题就消失了。
我有一个用户,它有一个 UserContact 列表,它本身有一个 ContactOption。这是一个相当简单的 1 对多,多对 1,中间有 UserContact 表。
如果我将用户从数据库中拉出并创建一个新的 UserContact,但将 ContactOption 设置为现有项目(我已将其从数据库中拉出),当我保存更改时, Entity Framework 会在基本上是我添加到 UserContact 的数据库的副本(除了它获得了一个新 ID)。
我已经为此苦苦挣扎了几个小时,还是想不通。有什么想法吗?
我正在为我的数据库查询使用存储库模式,但我已确保它们共享相同的上下文。
我用这个把用户从数据库中拉出来:
var user = _context.Users.Include("UserContacts.ContactOption")
.Where(id => id == 1);
联系选项被拉出:
var co = _context.ContactOptions.FirstOrDefault(c => c.Id == id);
然后我将 ContactOption 添加到 UserContact,如下所示:
var contactOption = _context.ContactOptions.FirstOrDefault(c => c.Id == someId);
var contact = new UserContact { ContactOption = contactOption };
contact.Data = "someData";
user.UserContacts.Add(contact);
我的模型是这样的:
public class User
{
[Key]
public int Id { get; set; }
public virtual ICollection<UserContact> UserContacts { get; set; }
}
public class UserContact
{
[Key]
public int Id { get; set; }
[Required]
public User User { get; set; }
[Required]
public ContactOption ContactOption { get; set; }
public string Data { get; set; }
}
public class ContactOption
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
最佳答案
我运行你的代码并得到了准确的预期结果:UserContacts 表中的新行包含现有的 UserId 和 ContactOptionId,所以我不确定那里发生了什么,但你可以尝试明确在 UserContact 对象中具有 FK,这样您就可以完全控制 Code First 为您插入记录的方式。为此,您需要按如下方式更改 UserContact:
public class UserContact
{
public int Id { get; set; }
// By Convention these 2 properties will be picked up as the FKs:
public int UserId { get; set; }
public int ContactOptionId { get; set; }
[Required]
public User User { get; set; }
[Required]
public ContactOption ContactOption { get; set; }
public string Data { get; set; }
}
然后您可以像这样更改您的代码:
var contactOption = _context.ContactOptions.Find(someId);
var user = _context.Users.Find(1);
var contact = new UserContact
{
ContactOptionId = contactOption.Id,
UserId = user.Id,
Data = "someData"
};
user.UserContacts.Add(contact);
context.SaveChanges();
关于c# - Entity Framework Code First 多对多创建重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4174232/
目前,我正在为网络开发类(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 → 全部 元素是其父元素的第一个子元
我是一名优秀的程序员,十分优秀!