How does EF codefirst know which model class to create a table for? - entity-framework

I was not able to find the answer to this online - please link me if I've overlooked any resources.
I understand how Entity Framework's codefirst works. The question is: how does EF know which model class to create a table for and which model class to just treat as a class?
For example, in the sample MVC4 application that comes from creating a new MVC project with VS 2012 Express Developer, there are classes (LocalPasswordModel, LoginModel, RegisterModel, etc) in the Account Model that have no tables, and EF knows not to generate tables for these classes.
How does EF know this?

Entity Framework looks at your DbContext class, and creates a table for each DbSet<T> property that you define.
EF won't even see any class which is not referenced by the DbContext.

Related

What is entity framework designer? Why it is used in the Model-first approach?

I know entity framework designer is used to create the classes of model-first approach and then we create the database by using that class. But in code-first appraoch, it is possible to create the custom classes, within that classes the database is created automatically. Then what is the difference between code-first approach and model-first approach?
Code First is the more modern style of working with Entity Framework. As the name implies, you write the code first and the database model is generated for you, by using Entity Framework Migrations. In this scenario you are not using any graphical tool at all, everything is just pure code.
Model first means creating the abstract database model in the designer. The code is then generated by templates. If you update the model, the code will be regenerated.

Entity Framework Code First model separation from domain

Entity Framework Code First best practice question?
Hi All I am using EF codeFirst 6 on an NTier app.
I have found that poco object that I am using to map to EF are really EntityFramework specific. Let me give you an example
If I want to add a property that is not related to EF in the object ,EF does not like it.
I Read you can put the "NotMapped" attribute however it start making this object difficult to maintain .
Also there might be developers that are not familiar with EF and that will not understand the issue.
My question is it good practice to keep EF Entity Models separate and have a dto to convert to/from to a Domain Model where
a developer can do what he likes with it without interferring with EF Model which is clearly a 1 to 1 with the tables in the database
Any Suggestions?
Your problem could be resolved by using the Fluent API approach instead of the Attribute-based (Annotations) approach. See Entity Framework Fluent API.
You would configure your entity mappings in the DBContext rather than in the entity classes.
From the above linked article:
Specifying Not to Map a CLR Property to a Column in the Database
The following example shows how to specify that a property on a CLR
type is not mapped to a column in the database.
modelBuilder.Entity<Department>().Ignore(t => t.Budget);
that would mean "ignore the Bugdet property in the Department entity."

How to cleanly generate POCO classes from existing database using Entity Framework 4.3 Code First approach?

I'm following the EF Code-First approach in a project that works against an existing database, to which I'm adding tables as needed.
This database has a number of tables for which I need to generate POCO classes for, and so I was wondering if there was a straight-forward, clean approach, to generating simple POCO classes from the database ... from which I can continue to work with using the general Code-First paradigm?
You can use the Entity Framework Power Tools for that.
If you want just the simple Poco classes without any relations use this T4 template
Generate entity class from database table

Entity Framework Recreate POCO class

This may seem like a silly question, but I thought I'd ask it anyway :)
When you use Entity Framework's Database First approach you can create an Entity Data Model to describe the structure of your business objects.
You can also use the ADO.Net DbContext Generator to create persistance ignorance POCO classes. However, when you make a change to the Data Model, ie add a new property to an existing Entity, in order for the corresponding POCO class to also reflect this change, do you have to:
Manually add the new property to the POCO class
Recreate all the POCOs again using the DbContext Generator
I guess what I am asking is there anyway the POCO can be automatically updated if a change is made to the Model?
Thanks everyone.
If you're using the T4 Templates that look at the edmx file, you can just regenerate the pocos by running the templates: Click the Transform Templates icon in the Solution Explorer?
)or have I missed something)

Entity Framework 4.0 Mapping POCOS with different property names from db fields names

I'm a newbie to ADO.Net Entity framework 4. I have a set of pocos which I need to map to a legacy database. The problem is that the db field names are different to the poco property names. eg. db field name = 'cusID' and poco property = 'CustomerID'.
What is the best way to map these?
This is exactly the problem EF mapping is designed to solve.
Your POCO class need to match your 'conceptual model'... Not your 'data model'.
If in EF you build your model from the database you simply need to rename your entity properties. Doing this changes the conceptual model - to match your POCO classes - but leaves the storage model unchanged, and sets up the appropriate mappings.
Entity Framework CTP4 has a new feature called Code First that allows you to map POCO property members to database table column names. This blog article may be what you are looking for,
http://theminimalistdeveloper.com/2010/07/28/how-to-map-pocos-to-existing-databases-in-entity-framework-4-0-code-first-and-asp-net-mvc-2/
Additionally, EF CTP 5 - which will be released in the next few weeks - has a better API to fluently configure your own conventions to map your POCO domain classes to existing database structures.
Hope this helps.
Update Here is the new article that discusses how to achieve this in EF4 CTP5