Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

asp.net mvc - Database Context View Objects Not Working as Expected With MVC 4

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.

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:

enter image description here

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.

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?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...