Mvc 3 脚手架:传递给视图的模型抛出 SQL 错误

发布于 2024-11-27 20:18:52 字数 3770 浏览 3 评论 0原文

这篇文章已被大量编辑和更新!

意图:

我正在编写一个应用程序,它本质上是一个迷你 ASP.NET MVC 3 会计包。我这样做是为了学习 EF 4.1 Code First 和脚手架

设置:

我正在使用 SQL Server 2008 Express、Visual Studio 2010 SP1 和 ASP.NET MVC 3 以及 Mvc Scaffolding 1.0.2。

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

Accounts
Banks
CostCentres
Currencies
DebitCredits
People
Transactions
TransactionTypes

有许多关系,例如 Person_Accounts 等。

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

我尝试过的:

我创建了一个 .edmx 并使用它通过 t4 自动生成来创建 POCO 类。一旦我有了 POCO 类,就排除了 .edmx。

解决了 EF 4.1 Code First 找不到它喜欢的连接字符串的问题,因此开始创建自己的 sql express 数据库(请参阅 Rachel Appels 博客了解有关此陷阱的详细信息

最终使用约定上下文名称=连接字符串名称首先获取 EF 代码以与正确的数据库对话。

然后我使用 MVC 3 脚手架来搭建视图。因此,生成的存储库代码不是我自己的,而是 Steve Sanderson 的。

我以前没有使用过 EF,因此希望这将是通过“观察和学习”从 LINQ 进展到 SQL 的一种方式。

事实证明,我遇到了一些问题...

问题:

首先,如果我使用 EF Code First 创建的数据库,则没有问题。

但是,将连接字符串更改为我以前存在的数据库(我用来创建 .edmx 文件),例如,当我请求为帐户实体搭建索引视图时,我现在会收到以下错误:

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 表。

--注释结束--

我的推理:

乍一看,这是一个非常奇怪的错误,因为尽管只想要一个没有任何相关数据的帐户列表,但发生的情况如下:

使用我预先存在的数据库或由我创建的数据库首先查看代码,当我检查 SQL Profiler 时,它显示一个条目 SQL: BatchStarting ,其中包含大量 SELECT 查询,该查询似乎正在选择数据库中的几乎所有内容。我不知道为什么调用这个大规模查询而不是对事务数据进行简单的选择。据推测,它正在尝试加载所有相关数据,但我没有要求这样做。

再次强调,使用代码首先生成的数据库,一切正常。但是使用我预先存在的数据库,它会抛出上面的错误。

这里有两个问题:

  1. 一个是错误。为什么会出现这样的情况?

  2. 另一个是针对数据库中几乎所有数据的巨大选择语句!!

我的观点只是试图吐出一个帐户记录列表。我对成本中心或货币表等没有兴趣(对此视图)

问题:

。 为什么脚手架存储库要求提供所有数据?

b.为什么在现有数据库中会发生错误?

我对这个问题设置了悬赏,谁回答了上面两个问题,谁就可以获得悬赏。

其他问题(与赏金无关!):

c。有谁知道博客链接,我可以在其中阅读如何在现有数据库中使用 MVC 3 脚手架和代码优先?

d.有没有办法使用 t4 模板来创建正确映射到现有数据库及其所有关系等的 DbContext 文件?

e.还有其他建议(不包括职业改变)吗?

f.有哪些书籍可以阅读 EF 4.1 Code First? (Julia Lerman 的最新版本是 EF 4.0,即 Code First 在发布时仅处于测试阶段)。

更新:

我已经回答了问题a(为什么巨大的查询会引入所有数据。脚手架存储库有一个方法:

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;
        }

从脚手架控制器调用:

//
        // GET: /Accounts/

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

我很抱歉。我太迷惑了。

但问题b仍然没有答案。

This post has been heavily edited and updated!

The Intent:

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

The Setup:

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

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

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

What I tried:

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.

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)

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

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.

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...

The Problem:

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

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'.

--Note:--

The only difference between database created by EF and the one created by me (dead simple) are the relationships and a couple of triggers, plus an EdmMetadata table.

--End of Note--

My Reasoning:

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:

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.

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.

Two problems here:

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

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

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.

The Questions:

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

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!):

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?

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. 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).

Update:

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.

But question b remains unanswered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2024-12-04 20:18:52

问题b:
您在预先存在的数据库列上收到错误的原因是它们与 codefirst 用于设置其自己的关系以使用数据库的列命名约定不匹配,因为它存在您需要在模型上使用流畅的映射或列属性要定义关系应该是

代码优先 api 设置的最佳方法,不是使用 edmx 来生成类,而是使用实体框架强大的工具 ctp 来执行此操作,

我也会尝试手动执行此操作生成后的前几次它可以学习流畅的 api 及其工作原理

,因为您可以使用几种不同的模式

来回答有关引入所有数据的问题,但它不会每次在编译时执行一次以创建数据文件时都会执行此操作然后,数据库中的所有内容都会跟踪它的更改,以提高性能,这样就不必每次问题 a 时都访问数据库


AllInclusion 函数允许您查询数据,如果您不想要这个,则将其删除并将其替换为如何查询数据

问题 d:

您可以编写自己的 t4 文件来生成什么你想要的任何代码,它们都是模板,专门为此目的而设计

问题f:

据我所知,目前还没有4.1特定的书籍,因为codefirst框架是全新的,我只找到了4.0版本的书籍
不过,据我所知,目前有 2 本 MVC 3 书籍涵盖了 CodeFirst 框架的某些方面,它们是专业的 asp.net mvc 3 (Wrox) 和专业的 asp.net mvc 3 (apress),并且都可以在亚马逊上找到每个大约35-40美元

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

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

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

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

question d:

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

question f:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文