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