本文介绍了MVC 3脚手架:传递到视图的模型抛出SQL errror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,本质上是一个小型的ASP.NET MVC 3会计软件包。我做它来学习EF 4.1 code首先脚手架

I am writing an app that is essentially a mini ASP.NET MVC 3 accounting package. I am doing it to learn EF 4.1 Code First and Scaffolding

我使用SQL Server 2008前preSS时,Visual Studio 2010 SP1和ASP.NET MVC 3的mvc脚手架1.0.2。

I am using SQL Server 2008 Express, Visual Studio 2010 SP1 and ASP.NET MVC 3 with Mvc Scaffolding 1.0.2.

我有一个现有的数据库。数据库具有以下表:

I have an existing database. The database has the following tables:

Accounts
Banks
CostCentres
Currencies
DebitCredits
People
Transactions
TransactionTypes

有一些关系,例如,Person_Accounts等

There are a number of relationships, eg, Person_Accounts, etc.

我现在想用MVC脚手架来为我的数据库中查找表创建数据创建一些输入页面。

I now want to use MVC Scaffolding to create some input pages for creating data for the look up tables in my database.

我创建了一个的.edmx,并使用了创建使用T4自动生成的POCO类。已排除的.edmx一旦我有POCO类。

I created an .edmx and used that to create the POCO classes using the t4 auto generation. Have excluded the .edmx once I have the POCO classes.

已经得到全面EF 4.1 code的问题第一次没有找到它喜欢的一个连接字符串,那么回事了,创造了自己的SQL前preSS数据库(见的)

Have got round the problem of EF 4.1 Code First not finding a connection string it likes, so going off and creating its own sql express database (see Rachel Appels blog for details on this gotcha)

终于使用的惯例上下文名称=连接字符串名称获取EF code先来谈谈正确的分贝。

Have finally used the convention context name = connection string name to get EF code first to talk to the correct db.

我已经然后用于MVC 3脚手架脚手架的意见。因此,所产生的资源库code是不是我自己的,但史蒂夫·桑德森的。

I have then used MVC 3 Scaffolding to scaffold the views. So the repository code that is produced is not my own, but Steve Sanderson's.

我以前没有使用EF,所以希望这将是从LINQ发展到SQL,通过望,学习手段的一种方式。

I haven't used EF before, so was hoping that this would be a way of progressing from LINQ to SQL, by means of "look and learn".

事实证明,我有一些问题...

Turns out, I'm having some problems...

首先,如果我用EF code创建数据库首先,​​没有任何问题。

First of all, if I use the database created by EF Code First, no problems.

但是,连接字符串更改到我的previously现有的数据库(我用来创建.edmx文件)我现在得到以下错误时,比如,我要求索引视图脚手架的账户实体:

But change the connection string to my previously existing database (that I used to create the .edmx file) I now get the following error when, for example, I request the Index view scaffolded for the Accounts entity:

Invalid column name 'Account_AccountId'.
Invalid column name 'Account_AccountId'.
Invalid column name 'Currency_CurrencyId'.
Invalid column name 'Transaction_TransactionId'.
Invalid column name 'Account_AccountId1'.
Invalid column name 'Account_AccountId'.
Invalid column name 'Account1_AccountId'.
Invalid column name 'CostCentre_CostCentreId'.
Invalid column name 'Currency_CurrencyId'.
Invalid column name 'TransactionType_TransactionTypeId'.
Invalid column name 'Account_AccountId1'.
Invalid column name 'Account_AccountId2'.
Invalid column name 'Account_AccountId2'.
Invalid column name 'Account_AccountId'.
Invalid column name 'Account1_AccountId'.
Invalid column name 'CostCentre_CostCentreId'.
Invalid column name 'Currency_CurrencyId'.
Invalid column name 'TransactionType_TransactionTypeId'.
Invalid column name 'Account_AccountId1'.
Invalid column name 'Account_AccountId2'.

- 注: -

由EF创建的数据库和由箱(死简单)是关系和一对触发器创建的,再加上一个EdmMetadata台之间的唯一区别。

- 注完 -

这样做的原因,在乍看之下,很奇怪的错误是,尽管只是想账户名单没有任何相关数据的,正在发生的事情如下:

The reason for this, at a first glance, very bizarre error is that despite just wanting a list of Accounts without any of the related data, what is happening is as follows:

使用或者我的pre现有的数据库或code首先,创建的,当我在SQL事件探查器检查,它显示了一个条目SQL:BatchStarting用,这似乎是选择刚才的一切大规模的SELECT查询在数据库中。我不知道为什么,而不是一个简单的选择对交易数据的这种大规模的查询被调用。 presumably,它试图加载所有相关的数据,但我还没有问那。

With either my pre existing database or the one created by code first, when I check in SQL Profiler, it shows an entry SQL: BatchStarting with a massive SELECT query that seems to be selecting just about everything in the database. I have no idea why this massive query is called as opposed to a simple select for the Transaction data. Presumably, it is trying to load all related data, but I haven't asked for that.

我再次强调,使用code首先产生的分贝,这一切的作品。但是,用我的pre已经存在的数据库,它抛出上述错误显示。

Again, I stress that using the code first generated db, it all works. But using my pre existing db, it throws the error show above.

在这里有两个问题:


  1. 一是错误。为什么会发生?

  1. One is the error. Why does it happen??

二是,对几乎所有在db!

The other is that huge select statement for just about all the data in the db!!

我的观点,只不过是想吐出的账户记录列表。我在CostCentres或货币表没有兴趣(这个观点),等等,等等。

My view is only trying to spit out a list of Accounts records. I have no interest (for this view) in the CostCentres or Currencies tables, etc, etc.

一个。为什么脚手架库要求所有数据?

a. Why is the Scaffolded repository asking for ALL the data?

乙。为什么在pre现有数据库发生错误?

b. Why is the error happening in the pre existing database?

我已经设置在这个问题上的赏金,谁能回答上述两个问题将得到赏金。

I have set a bounty on this question, and whoever answers the above two questions will get the bounty.

其他问题(不涉及赏金!):

Other questions (not related to the bounty!):

℃。有谁知道链接的博客,我可以对我应该做与现有数据库首先使用MVC 3脚手架和code什么读了?

c. Does anyone know of links to blogs where I can read up on what I should do to use MVC 3 scaffolding and code first with an existing database?

Ð。有没有使用T4模板来创建的DbContext文件正确映射到现有的数据库,其所有的关系,办法等?

d. Is there a way to use the t4 templates to create a DbContext file that correctly maps to the existing database, with all its relationships, etc?

即任何其他建议(不包括职业生涯的变化)?

e. Any other suggestion (excluding career change)?

F。任何书籍在EF 4.1 code首先读了? (朱莉娅·勒曼最新的EF为4.0,也就是说,code第一次是只在测试版发布时)。

f. Any books for reading up on EF 4.1 Code First? (Julia Lerman's latest is EF 4.0, ie, Code first was only at beta at time of publication).

我已经回答了一个问题(为什么庞大的查询海纳百川的数据存储库脚手架有一个方法:

I have answered question a (Why the huge query bringing in all the data. The scaffolded repository has a method:

public IQueryable<Account> AllIncluding(params Expression<Func<Account, object>>[] includeProperties)
        {
            IQueryable<Account> query = context.Accounts;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

这会从脚手架控制器名为:

That gets called from the Scaffolded controller:

//
        // GET: /Accounts/

        public ViewResult Index()
        {
            return View(accountRepository.AllIncluding(account => account.Person, account => account.DebitCredits, account => account.Transactions, account => account.Transactions1));
        }

我的道歉。我太迷惑。

My apologies. I was too bamboozled.

但问题B仍然没有答案。

But question b remains unanswered.

推荐答案

问题B:
您收到的preexisting数据库列错误的原因是,他们不匹配列命名约定codefirst使用设置自己的关系,因为它的存在,你需要用流利的映射使用数据库或您的模型列属性来定义的关系应该是什么

question b: the reason you are receiving the error on the preexisting database columns is that they don't match the column naming conventions that codefirst uses to setup its own relationships to use the database as it exists you need to use fluent mapping or column attributes on your models to define what the relationships should be

要为code第一个API设置做到这一点,最好的办法是不要使用EDMX生成您的班级使用实体框架电动工具CTP要做到这一点,而不是

the best way to do this for a code first api setup is not to use an edmx to generate your classes use the entity framework power tools ctp to do this instead

我也尝试做手工这是你第几次产生它来学习流畅API后它是如何工作

i would also try to do this by hand your first couple times after generating it to learn the fluent api and how it works

因为有你可以用它使用几个不同的模式

as there are several different patterns you can use with it

要回答你的问题有关海纳百川的数据不做到这一点在编译的时候做这个曾经创造一切的数据文件在数据库中,然后跟踪它的变化之后,要提高性能,因此它没有每次每次打数据库

to answer your question about bringing in all the data it does not do this every time it does this once at compile time to create a datafile of everything in the database then tracks it for changes after that to improve performance so it doesnt have to hit the data base every time

问题一:
AllIncluding 功能是那里让你,如果你不想这样,然后将其删除,并希望如何对数据进行查询替换查询数据

question a:the AllIncluding function is there to allow you to query the data if you dont want this then remove it and replace it with how want to query the data

问题D:

您可以编写自己的T4文件生成什么都code你想他们的模板和专门为此设计的。

you can write your own t4 files to generate what ever code you want they are templates and designed specifically for this purpose

问题F:

有没有具体的4.1出来的书,我知道作为4.0版本还为codefirst架构是全新的我只找到的那些
但也有2 MVC 3本书出来目前,我知道该做的覆盖codefirst框架,它是专业的asp.net MVC 3(Wrox出版)的某些方面和亲asp.net MVC 3(A preSS ),并都可以在亚马逊的35-40雄鹿每个

there no 4.1 specific books out that i know of as of yet as the codefirst framework is brand new i have only found ones on version 4.0 however there are 2 mvc 3 books out currently that i do know of the do cover some aspects of the codefirst framework which are professional asp.net mvc 3 (Wrox) and pro asp.net mvc 3 (apress) and are both available on amazon for about 35-40 bucks each

这篇关于MVC 3脚手架:传递到视图的模型抛出SQL errror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:31