System.InvalidOperationException:附加类型为“ QC.Models.Defect”的实体失败,因为另一个相同类型的实体

时间:2018-09-27 06:14:39

标签: c# entity-framework ef-code-first

看看我的代码

sudo tail -f /var/log/nginx/error.log
[sudo] password for saeb: 
2018/09/27 00:15:50 [error] 2650#2650: *13 open() "/usr/share/nginx/html/robots.txt" failed (2: No such file or directory), client: 54.175.74.27, server: bot.itarashe.zone, request: "GET /robots.txt HTTP/1.1", host: "bot.itarashe.zone"
2018/09/27 00:27:23 [error] 2650#2650: *16 open() "/usr/share/nginx/html/robots.txt" failed (2: No such file or directory), client: 66.249.66.76, server: bot.itarashe.zone, request: "GET /robots.txt HTTP/1.1", host: "oldflight.ir"
2018/09/27 00:27:23 [error] 2650#2650: *17 open() "/usr/share/nginx/html/assets/font/IranSans/IRANSans-Medium-web.woff2" failed (2: No such file or directory), client: 66.249.66.75, server: bot.itarashe.zone, request: "GET /assets/font/IranSans/IRANSans-Medium-web.woff2 HTTP/1.1", host: "oldflight.ir"
2018/09/27 01:09:02 [notice] 2811#2811: signal process started
2018/09/27 01:27:45 [notice] 2831#2831: signal process started
2018/09/27 01:42:01 [notice] 2876#2876: signal process started
2018/09/27 01:42:41 [notice] 2889#2889: signal process started
2018/09/27 01:43:54 [notice] 2902#2902: signal process started
2018/09/27 01:44:38 [notice] 2915#2915: signal process started
2018/09/27 01:50:06 [notice] 2936#2936: signal process started

当我附加用户和订单时,即使这样的对象及其主键存在于数据库中也能正常工作(这就是为什么我附加的原因,我不会创建一个新对象,只需将其附加到ENTRY) 。但是,当我尝试附加缺陷时,也会引发该using (MyBridgeContext context = new MyBridgeContext()) { context.Users.Attach(user); //both working fine context.Orders.Attach(order); foreach (Defect def in defectList.Items) { if (defectList.SelectedItem == def) { context.Defects.Attach(def);//throwing error defect = def; } } DefectEntry entry = new DefectEntry(); entry.user = user; entry.defect = defect; entry.order = order; entry.dt = DateTime.Now; context.Entries.Add(entry); context.SaveChanges(); this.Content = new MainMonitoring(ref order, ref user); } <br /> 错误。

因此,这是Defects组合框:(程序加载缺陷列表,当用户选择缺陷列表时,应将其附加到条目中):

System.InvalidOperationException

这是MyRepository:

private void loadDefects()
    {
        MyRepository rep = new MyRepository();
        var defects = rep.GetDefects();
        foreach(var def in defects)
        {
            defectList.Items.Add(def);
        }
        defectList.DisplayMemberPath = "Name"; //defectList is comboBox
    } <br /> <br />

0 个答案:

没有答案