entity framework working - asp.net-mvc

I would generate entity framework model of my whole database or do it scenario based with 2-5 tables entity model generation? What is the best thing to do

Generate only the tables needed by the application. Then write wrappers around it based on scenario.

Related

What exactly is the difference between code first and database first approach in mvc

I don't know when use code-first and database-first approach.
I am already working with both code-first and database-first, I know how they work but my question is when to use code-first and database-first?
Today I had an interview and the interviewer asked me what the difference between code-first and database-first approach is.
Me: when you working with small application then use code first and when you working with large application then use database first
Again interviewer asked me what the difference between code first and database first approach is.
Me: in code first approach you don't need to create a table means design a table in class and add a connection string in web.config and database first approach you need to create a table and database
Again interviewer asked me what the difference between code first and database first approach is.
me:silent
Again interviewer asked me which situation I use code first and database first approach
me:silent
when you working with small application then use code first and when you working with large application then use database first
Your answer is a fair rule-of-thumb, but this is incorrect because it doesn't answer the question: "what is the difference`. This answers the unasked question "when would you use code-first vs. database-first" (though your interviewer did ask this question right at the very end).
in code first approach you don't need to create a table means design a table in class and add a connection string in web.config and database first approach you need to create a table and database
This is correct - but you're describing it in-terms of the developer-experience, rather than what Entity Framework is actually doing behind-the-scenes.
A better description of the differences would be:
Database-first:
The database is the canonical definition of the EF data store design.
So you should store your database design as an SSDT project in source-control!
EF is also aware of your C# entity types and maps your SQL database to your C# types using an *.edmx file.
Even though EF6 and EF Core no-longer use an EDMX file in your codebase, EF still uses EDMX internally.
Code-first:
Your C# entity types are the canonical source-of-truth for the EF data store, and EF handles instantiating the actual SQL database.
Note that you can use EF Migrations and entity type Scaffolding with both Database-first and Code-first approaches.

ASP.NET MVC create models in fast way by Entity framework?

I am working in ASP.NET MVC 5 with Entity Framework 6.1 in Database First approach.
I starting create Models Class response to each Entity (the tables of database),
I feel it is boring task and found that Entity Framework almost done for me.
I expand the *.edmx file and then expand *.tt file, there are many class there and 90% similar to my hand made models.
So I start search on Google to see what is the best practice to generate models in smart way. Finally, the following 2 ways I found.
1. Copy all the class corresponding to each database's table to Models folder and remove this line:
System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
2. Using EF 6.1 Wizard - Code First from Database
Which similar to method 1 but come with addition DataAnnotations.
There is a 3rd method recommended by Microsoft Doc,
Generate the Context with Models inside Models folder and work with, the most simple way, but we know this is the worst one.
So Which method I mention above is proper way to create models?
Data base First approach is the best way to create model classes but u don't want annotations for model class at this time you may select 'EF Designer from Database' option from EF wizard choose model step

asp.net mvc with entity framework database first domain model validations

I'm Developing ASP.NET MVC Web Application project, and I'm using Entity Framework Database First Approach, So I would like to make validations on generated model classes, I know if i make validations on them directly, then my model validations will be overwritten every time my domain models are regenerated.
So I made a research, and found two approaches to use for this scenario:
1- Using buddy classes (How to add validation to my POCO(template) classes).
2- Using ViewModels and Auto-mapping them to my Entities (Designing an MVC repository using ViewModels
I see some sort of redundant code in these two methods, so, my question is:
Which one of the two approaches is best to flow?
1) This is the correct solution for adding validation metadata for the Entity Framework objects. The validation will be triggered automatically by EF before calling SaveChanges()
2) This is an aproach for creating Data Transfer Objects from your EF objects. You normally do this when you want to return the objects to the client (like in JSON format) - and you don't want to expose all the EF specific properties (like navigation properties, primary keys etc)

How to modify CRUD operations with Entity Framework in .NET MVC application

I need to work out how to do CRUD stuff in an MVC application, passed to me by a former colleague (I don't have any other info, just the application and the database). So I can see there is Model1.edmx and the model browser that contains MyApp.Model>EntityTypes>MyTable representation, and MyAppModel.Store that contains a representation of the table, and the Model1.Designer.cs file which has methods that look like they must be CRUD related (e.g. OnLastNameChanging, OnLastNameChanged). I can run the application and insert and update records to a db table.
What I need to know is where / how do I code other CRUD operations and use Entity Framework to work with WHERE clauses, and do stuff like update another record in a table depending on the value of a given field in the record being inserted or updated.
I've worked with MVC on one other small project but haven't really worked with entities. I'm used to the WebForms / ADO.NET / stored procedures way of doing things.
Any help gratefully received.
Happy new year!
I guess you are using database first approach.
some useful links for Code first approach:
http://www.codeproject.com/Tips/793159/Code-First-Approach-in-Entity-Framework-using-Data
other for database first approach
http://www.aspdotnet-pools.com/2014/08/insert-update-delete-operation-in.html
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

EF 4.x generated entity classes (POCO) and Map files

I have an MVC 4 app that I am working on and using the code first implementation except I cheated a bit and created my database first then generated my entity classes (poco) from my database using the EF power tools (reverse engineer). I guess you can say I did database first method but I have no edmx file just the context class and my entity classes (poco)
I have a few projects in the works using MVC and EF with pocos but just the one project I used the tool to generate my pocos from the database.
My question is about the mapping files that get created when I generate my pocos using the tool. What is the purpose of these Map files? I figured the map files are needed when generating the db from the model like with the true code first method, in my case where I am using a tool to generate my model from the database do the map files have any influence on how my app uses the entity classes?
The mapping files are to fluent files help Code First generate the database from your model, as well as help EF create the proper relationships.
They can map properties - things like setting a primary key, max length, data type.
They can also map relationships - set things like foreign keys, and defining relationships that don't follow standard CF naming conventions.
Even though you're not generating your database from the app, CF will use this system to ensure that the database it's pointing to is compatible with your model, and in the case of foreign keys and things setup related properties. For example, if you wanted to name a FK something other than NavigationPropertyId, you would need fluent to tell the engine what property to set in the database.

Resources