数据库上下文视图对象未按预期随着MVC 4 [英] Database Context View Objects Not Working as Expected With MVC 4

查看:123
本文介绍了数据库上下文视图对象未按预期随着MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是实体框架观MVC时遇到我的code一个非常奇特的问题。我只是怀疑的问题可以归结为使用视图,因为使用表时,我没有问题。

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和控制器code的指数为:

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个记录的数据将是第一个记录的重演。因此,在这种情况下,所有3条记录将有700 PackageAuditId的同一字段值。

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接口时它涉及到意见我从来没有见过这个问题?

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意见或避免有更多的东西,我应该考虑与他们合作的时候?

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的主键。如果密钥由场不属于真正独特的,你可以得到结果集中的重复行。所以,你应该调查的看法,并决定哪些列唯一标识其行,并告诉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天全站免登陆