Mvc 3 Scaffolding: Model passed to View throws SQL error

This post has been heavily edited and updated!

Goal:

I am writing an application that is essentially a mini ASP.NET MVC 3. I am doing this to learn about EF 4.1 Code First and Scaffolding

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 several relationships, e.g. Person_Accounts, etc.

Now I want to use MVC Scaffolding to create some input pages to create data to look up tables in my database.

What I tried:

I created .edmx and used this to create POCO classes using the t4 auto generation. Excluded .edmx when I have POCO classes.

Corrupted the problem EF 4.1 Code At first I didnโ€™t find the connection string that I like, so it leaves and creates its own sql express database (see the Rachel Appels blog for more details about this game )

Finally, the convention context name = connection string name was used to get the EF code first to talk to the correct db.

Then I used MVC 3 Scaffolding to render views. Thus, the generated repository code is not mine, but Steve Sanderson.

I had not used EF before, so I hoped that this would be a way of moving from LINQ to SQL with a โ€œlook and learnโ€.

It turns out I have a problem ...

Problem:

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

But change the connection string to my previously existing database (which I used to create the .edmx file). Now I get the following error when, for example, I request the โ€œView Indexesโ€ stand for the Accounts object:

 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 the database created by EF and the one created by me (dead simplicity) is the relationship and several triggers plus the EdmMetadata table.

- End of note -

My reasoning is:

The reason for this, at first glance, a very strange error is that, despite the simple receipt of a list of accounts without any related data, the following occurs:

When either my pre-existing database, or the first one created by the code, when I check the SQL Profiler, it shows an SQL: BatchStarting record with a massive SELECT query that seems to select almost everything in the database. I do not know why this massive query is called, unlike a simple choice for transaction data. Presumably, he is trying to download all the related data, but I did not ask for it.

Again, I emphasize that using code first created by db, everything works. But using my previous db, it throws the error message above.

Two issues here:

  • One of them is a mistake. Why is this happening?

  • Another is that the huge select statement for almost all data in db!

My opinion is only trying to spit out a list of account entries. I have no interest (for this view) in CostCentres or Currencies tables, etc. Etc.

Questions:

but. Why does the repository for forests have ALL data?

b. Why does an error occur in an existing database?

I put generosity to this question, and the one who answers these two questions will receive a reward.

Other issues (not related to generosity!):

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

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

e. Any other offer (excluding career change)?

f. Any books to read on EF 4.1 Code First? (Julia Lerman latest version of EF 4.0, that is, the code was initially only in beta at the time of publication).

Update:

I answered the question a (Why is a huge query causing all the data to appear. The forest 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; } 

It is called from the forest 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 bamboo.

But question b remains unanswered.

0
asp.net-mvc-3 ef-code-first asp.net-mvc-scaffolding
Aug 05 '11 at 9:45 a.m.
source share
1 answer

question b: the reason you get the error in the previous columns of the database is because they do not follow the column naming conventions that codefirst uses to configure their own relationships to use the database, since it exists, you need to use it freely mappable or column display attributes on your models determine what relationships should be

the best way to do this for the first installation of api for code is not to use edmx to generate your classes, use ctp instead to manage the entity infrastructure to do this instead

I would also try to do this manually for the first two times after creating it, to learn the current api and how it works.

as there are several different patterns that you can use with it

to answer your question about entering all the data that it does not, every time it does it once at compile time, to create a data file of everything in the database, then tracks it for changes after that, to improve performance, therefore he does not have to click on the database every time

question a: the AllIncluding function allows you to request data if you do not want it, then delete it and replace it with the way you want to request data

question d:

you can write your own t4 files to generate the code you want it to be a template and designed specifically for this purpose.

question f:

there are no 4.1 specific books that I know so far, since the concept of codefirst is completely new, I only found it in version 4.0 however there are currently 2 mvc 3 books that I really know that I am considering some aspects of the codefirst structure. which are professional asp.net mvc 3 (Wrox) and pro asp.net mvc 3 (apress), and both are available on amazon for about $ 35-40 each

+3
Aug 13 '11 at 16:56
source share



All Articles