.net 传递的主键值的数量必须与实体上定义的主键值的数量相匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27150575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:51:25  来源:igfitidea点击:

The number of primary key values passed must match number of primary key values defined on the entity

.netsql-serverasp.net-mvcentity-frameworkedmx

提问by Curiosity_is_Learning

I've created a view in SQL Server that contains the most important columns from different tables. Printing the content of the table to the ASP.NET MVC view works fine, but when I want to get the details of a single record the problem occur.

我在 SQL Server 中创建了一个视图,其中包含来自不同表的最重要的列。将表的内容打印到 ASP.NET MVC 视图工作正常,但是当我想获取单个记录的详细信息时,就会出现问题。

The number of primary key values passed must match number of primary key values defined on the entity.

传递的主键值的数量必须与实体上定义的主键值的数量相匹配。

I navigate to the specific record doing this:

我导航到执行此操作的特定记录:

@Html.ActionLink("Details", "Details", new {  id=item.Record_number  })

Record number is the primary key. I set this manually by right clicking on the specific variable in the .edmxmodel. Then I try to get the specific data using the following:

记录号是主键。我通过右键单击.edmx模型中的特定变量来手动设置。然后我尝试使用以下方法获取特定数据:

    //
    // GET: /Record/Details/5
    public ActionResult Details(int id = 0)
    {
        try
        {
            RecordDataView record = db.RecordDataView.Find(id); //HERE THE ERROR OCCUR
            if (record == null)
            {
                return HttpNotFound();
            }
            return View(record);
        }
        catch(EntityException)
        {
            return RedirectToAction("NoDatabaseConnection", "Home");
        }
    }

And the model look like this:

模型看起来像这样:

namespace Implant.Database
{
    using System;
    using System.Collections.Generic;

    public partial class RecordDataView
    {
        public int Record_number { get; set; }
        public Nullable<System.DateTime> DOB { get; set; }
        public Nullable<System.DateTime> Record_date { get; set; }

        /** rest of code omitted */ 
    }
}

At the moment I am using the following code to make it all work. But I don't feel this is a very good way or efficient. And I am very curious how to fix the failure above!

目前我正在使用以下代码使其全部工作。但我不觉得这是一个很好的方法或效率。我很好奇如何解决上面的故障!

    //
    // GET: /Record/Details/5
    public ActionResult Details(int id = 0)
    {
        var record = from r in db.RecordDataView
                     select r;
        if (id != 0)
        {
            record = record.Where(r => r.Record_number == id);
        }
        RecordDataView rec = record.ToList().First(); 

        return View(rec);   
    }

Someone got any idea why this error occurs? Thanks for help!

有人知道为什么会发生此错误吗?感谢帮助!

回答by Marcin

If you set your Primary Key in .edmxyou should update your database too, because you have PK in your model and not in your database. Update: does not work for views.

如果您在其中设置了主键,.edmx您也应该更新您的数据库,因为您的模型中有 PK 而不是数据库中。 更新:不适用于视图。

For views use .SingleOrDefaultinstead of Find().

对于视图使用.SingleOrDefault而不是Find().

In this scenario change the following line: RecordDataView record = db.RecordDataView.Find(id);To the following: RecordDataView recorddataview = db.RecordDataView.SingleOrDefault(m => m.Record_number == id);

在这种情况下,更改以下行:更改为以下RecordDataView record = db.RecordDataView.Find(id);内容:RecordDataView recorddataview = db.RecordDataView.SingleOrDefault(m => m.Record_number == id);

回答by Seun S. Lawal

If your database table doesn't specify a primary key field, your edmx model will assume each field is a key. So ensure your database and your entity model as saying the same thing.

如果您的数据库表没有指定主键字段,您的 edmx 模型将假定每个字段都是一个键。因此,请确保您的数据库和实体模型说的是同一件事。

If your model and database correctly specify the primary key, then, you can use .Find() and it will work.

如果您的模型和数据库正确指定了主键,那么您可以使用 .Find() 并且它会起作用。