数据库上下文视图对象使用MVC 4不能正常工作 [英] Database Context View Objects Not Working as Expected With MVC 4

查看:112
本文介绍了数据库上下文视图对象使用MVC 4不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC中使用实体框架视图时,我的代码中遇到了一个非常特殊的问题。我只是怀疑这个问题归因于使用View,因为在使用表时没有问题。

I am experiencing a very peculiar issue in my Code when using an Entity Framework View in MVC. I am only suspecting the problem to be attributed to the use of the View because I don't have the issue when using Tables.

我的视图称为LoadAuditStats_Report,索引的控制器代码为:

My View is called LoadAuditStats_Report and the Controller Code for the Index is:

    private readonly RepositoryDBContext _repository = new RepositoryDBContext();
    public ActionResult Index()
    {
        var stats = _repository.LoadAuditStats_Report;

        return View(stats);
    }

视图中的数据示例将是:

An example of the data in the view would be:

如果我从控制器检查示例的结果输出,我将获得3条记录,但所有3条记录的数据将重复第1条记录。所以在这种情况下,所有3个记录将具有与PackageAuditId 700相同的字段值。

If I examine the resulting output of the example from the Controller I will get 3 records but the data for all 3 records will be a repeat of the 1st record. So in this case, all 3 records will have the same field values of PackageAuditId 700.

这真的很棒,因为我在使用表时从未看到这个问题,所以我不要怀疑EntityFramework4与MVC3的连接是否与Views有关?

This is really wierd because I've never seen this problem when using Tables so I have to wonder if there is some issue with EntityFramework4 interfacing with MVC3 when it relates to Views?

我应该禁止使用EntityFramework Views还是有其他的东西在使用它们时应该考虑一下?

Should I refrain from the use of EntityFramework Views or is there something additional that I should be taking into consideration when working with them?

推荐答案

视图还有一个主键。如果键由字段组成,不是真正唯一的,您可以在结果集中获得重复的行。因此,您应该调查该视图,并确定哪些列唯一标识其行,并告诉EF使用这些列作为主键。

A view also has a primary key in EF. If the key consists of field that are not really unique you can get duplicate rows in the result set. So you should investigate the view and decide what columns uniquely identify its rows and tell EF to use those columns as the primary key.

这篇关于数据库上下文视图对象使用MVC 4不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆