C# interview questions approaches in entity Framework? - c#

In my recent interview, my interviewer we asked about approaches in entity Framework I told them code first and table first. Is there any approaches pending.

We can use three type of entity framework approach as per project requirement.
Database First:
An existing database can be used
Code can be auto-generated.
Extensible using partial classes/ T4 templates
The developer can update the database manually
There is a very good designer, which sync with the underlining database
http://www.entityframeworktutorial.net/database-first-with-entity-framework.aspx
Code First:
There is full control of the model from the Code; no EDMX/designer
No manual intervention to DB is required
The database is used for data only
http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx
Model First:
Good support with EDMX designer
We can visually create the database model
EF generates the Code and database script
Extensible through partial classes

Related

Entity framework adding existing classes to Entity Framework Model Diagram

Hi I'm new to Entity Framework so excuse me if this question seem simple, but I have tried to find an example or question where this is described.
I have built a webApi interface with classes in what I call an eventmodell. The Interface and model is working well to receive stuff. But now I would like to build the CRUD functions to the database with Entity Framework.
I have added an empty code first model, but then I don't now how to generate csdl, msl and ssl files needed to create the database.
I have added Empty EF designer model but have found no way of adding an existing class to the designer.
I have installed EF Power Tools but that didn't help.
You seem to have a misunderstanding on how Code First works as compared to Database First.
There is no designer needed in a Code First approach. You add classes to your model, setup a DbContext and use Migrations to setup, and then at later stages, alter the database structure.
Checkout this tutorial: Getting Started with Entity Framework 6 Code First using MVC 5

Entity Framework EDMX to Code First

I have an application using EF5 modeling with EDMX and I have much more experience and affinity with Code First. Does anyone have any idea / tutorial on how to migrate from EDMX Code First without changing the database and entity classes?
You can generate Code First classes from existing database. For this you should create new ADO.NET EDM project and choose "Code First from database" option. After that you choose your DB instance and tables for generating DTOs.
More details in https://msdn.microsoft.com/en-us/library/jj200620.aspx

entity framework model to database with indexed properties

I'm using EF 5.0 and diagram modeler (*.edmx) to create schema and generating database from model, and I was wondering if there is any way to create index on some property (only) via this modeler.
Many thanks for answer or advice.
//edit
I'm working on it, if I success, I'll share a little "how to".
Here is an article about Model-First in the Entity Framework 4
Read the sections about Generating the DDL and Influencing the DDL Generation
Update
Take a look at this example too: Entity Framework Part 5 Model First : Create a database with the Complex Types.

Given a database schema, programmatically generate EF source code

We have a project driven by database design. And when someone makes a db modification, I would like to have my EF code updated. So, I was wondering if there is a way to programmatically generate EF source code given a database connection string. I then plan on attaching this generated EF source code to my solution. I don't need an exact solution right now, but if anyone can point me in the right direction, that would be great.
Thanks!
You could query the SQL Server system tables and generate code that way, perhaps using XML and XSLT or T4 templates.
http://msdn.microsoft.com/en-us/library/ms189082(v=sql.105).aspx
There are three approaches to Entity Framework Development: Database First, Model First, and Code First. You are using Database first.
Entity Framework Development Approaches
I recomend Julia Lerman's book Programming Entity Framework 2nd Edition.
The tutorial I've linked to is for Code First but it has a nice introduction to Repository and Unit Of Work Patterns which you will want to know when using any of the three approaches.

C# class to Sql table

There are a lot of Sql table -> C# class methodologies, but I'm looking for the reverse.
Hypothetical situation:
I have N classes populated by some web service I consume, manipulate, then preform an action on. Now the boss wants said web service data persisted in a database.
I already have the classes defined, how can I quickly and easily (aka, lazily) generate a sql table off of each class?
Entity Framework and nHibernate both allow you to use them in "code-first" mode, which involves writing classes then generating databases.
There is a walk-through of this on ScottGu's blog here: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
you can use MS Entity Framework Code First approach:
Code First allows you to define your model using C# or VB.Net classes,
optionally additional configuration can be performed using attributes
on your classes and properties or by using a Fluent API. Your model
can be used to generate a database schema or to map to an existing
database.
read more about it here: EF 4.1 Code First Walkthrough
Entity framework 4 has a code first development feature, I haven't used it so can't really recommend it
ScottGu blog

Categories

Resources