Question

I'm new to MVC, and my intention actually is to learn 'Code First' technique, using MVC 4. I'd been through several 'MVC 4 Entity Framework' tutorials, and no doubt, tutorials are really simple, and understandable. but for every tutorial I followd, the

EF will look for the database in the default location and if it doesn’t exist, it will create it for you

part is not working for me. Do i have to add ADO.net Entity framework model in project, and design the database entities firt? or am I missing something? It's been more than 15 days, & I'm stuck.

(I tried making database manually first, and it worked perfect. But I want to learn, as described in tutorials, that databases are automatically created, if we mention the connection string, and EntityFramework finds out that path doesn't actually contains such database, so it creates one).

I'd been through several tutorials, let's say, I'm following THIS one. (I'm using Visual Studio Express for web 2012, EF 5.0.0)

Was it helpful?

Solution

your question: Do i have to add ADO.net Entity framework model in project, and design the database entities firt?

yes, in code first approach you need to tell entity framework what tables you are going to create, you do it by creating poco classes(Plain old CLR Object). then in run time entity framework will create a model(witch is a xml file) and generate query according to that xml file then executes it. you can read more about that in [CSDL MSL CSDL][1]

so for summary you write your domain classes(poco classes) and ef will create db for you but btw there is another way where in entity framework code first witch is called [Code first to existing database][2]

[2]: http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx it revers engineer the databse and create model for you.

OTHER TIPS

Do i have to add ADO.net Entity framework model in project, and design the database entities first?

This is really the more traditional approach, aptly called the database-first approach. The code-first approach is an alternative to the former. They mean exactly what they say. In the case of code-first the code is first written and in effect the database is generated automatically. In database approach, the database is first created then you can use EF to automatically generate the code required to handle the database, mainly CRUD and connection management.

The built-in scaffolder in ASP.NET MVC should work fine either in code-first or database-first as long as you have your models already established in your project. If you find the generated code too simplistic, an alternative scaffolder that you can use is CamoteQ - it is an online ASP.NET MVC scaffolding tool that is based in DDD principles and recognized patterns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top