What is the use of dbcontext generator in EF 6 - c#

I have a MVC project that use Entity Framework 6. I am puzzled as how it is created. Because there is no .edmx file, I guess it must be created as EF 6.x dbcontext generator. So what does dbcontext generator do? Does it create database from the model? If so, how to create database in the model? May I modify the database later and update the model? What are changed in the sources? Thanks.

It's difficult to understand everything you're saying, but it sounds like you have a project which uses the "Code First" approach. There is no .edmx file in this approach. Entity Framework Code First can be used with both new (see: https://msdn.microsoft.com/en-us/data/jj193542.aspx) and existing databases (see: https://msdn.microsoft.com/en-us/library/jj200620.aspx) and with regards to updating it, you could either manually modify the database or you could look into something called "Code First Migrations" (see: https://msdn.microsoft.com/en-us/data/jj591621.aspx), which allows you to add/remove tables and fields via code.

Related

How to mix Database first and Code first approach

I have a new MVC website (Internet Application) created using VS 2013.
I come from a Database first background and would like to take this approach for this project.
The project from what i see already has Entity Framework installed but i don't see any Entity Data Model files most likely because the project is Code First by default.
How could i bring the Database First approach in this project (the tables are already created within the database)?
Adopt the "Code First to an Existing Database" workflow, which is really a database-first workflow.
That will allow you to add a generated DbContext that maps to your existing database, without introducing an .EDMX file and the old designer-based database-first workflow.
The old EDMX-based database-first workflow should not be used for new work if you can help it. There's a lot of obsolete design and complexity in the OSpace/CSpace/SSpace mapping that Code-First hides from you, and EF Core has eliminiated entirely. There's some functionality in EDMX that hasn't been replicated in code-first, but there are reasonable workarounds for most of it.

Create new database using Entity Framework Core, npgsql, PostgreSQL

Newb here sorry. I'm reading up Entity Framework Core, npgsql and PostgreSQL (new to all of these things!!) Can someone please very kindly explain or point me where to read: I am trying to create a brand new database using code.
From the things I have read, I am creating the DB using pgadmin or script directly. What I want to do is create a database all via code (so the user can install Postgresql and my website but not have to worry about any direct SQL commands with PostgreSQL).
Thank you!
There are two ways to have EF Core create the database:
Use the lightweight EnsureCreated() method, which simply creates your database base on the model. If you're just starting out, it's probably best to start here, see the docs.
Use migrations, which also take care of updating your database schema as your code model changes. For example, if you add a property to one of your C# classes, EF Core can automatically generate a migration that will add the corresponding column to the table. Here are the docs for that.
The general docs for this are here. As a general rule, read and understand the EF Core docs first, then you can look at database- and provider-specific details (e.g. PostgreSQL/Npgsql).

entity framework use code first on single new schema in existing database

I have a new project I am starting and I would like to use entity framework 6 code first to handle the database portion. The problem is, I do not have permissions to create a new database on the server I need to use. Because of this, I would like to create a new schema in an existing database and then have entity framework only interact with that specific schema. There are many other tables in other schema in the database that are related to other projects (none of which make use of entity framework in any way) and entity framework needs to leave them alone when it is creating/dropping/modifying tables related to my project.
Is that possible? If so, how do I go about setting that up?
It turns out this is actually pretty easy using EF6! In your context just override OnModelCreating and add
modelBuilder.HasDefaultSchema("schema_name");
before the call to
base.OnModelCreating(modelBuilder);

Entity framework - code first, or maybe model? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
EF 4.1 Code-first vs Model/Database-first
i made some research, but answers did not satisfy me.
I started my own asp.net mvc application with entity framework, to work with database. I started by making a db, than i create model from that database. Well i know thats called "model-first". I know the second way, of doing it from school (code first). My lecturer told me today, that model-first method is rly outdated. Well is that true? Or maybe: does code-first have any advantage? Whats the real difference, which one is better, in your opinion?
Specifically with EF code first refers to the code and model first refers to the database.
which is better really depends on the project. If the db is treated as the core of the application, or you are using db specific features, than a model first approach makes sense.
If the domain model is treated as the core of the application, leaving the db to be just a form of persistent storage, than a code first approach is practical.
as a side note: this concept is usually referred to as model first (the domain/code) and db first (the db schema). with EF this is somewhat confusing as model refers to the db schema, not the domain model and code refers to the code.
DB first is not outdated at all. In fact, for many corporate projects that's the way to go.
If you are building something for yourself from scratch, code first will probably be a better approach.
Entity Framework provides: 1)Database first, 2)Model first and 3) Code first.
1 & 2 Creates the edmx file
Database first: Entity Data Model(edmx:StorageModels) is created from an existing database.
Model first: Conceptual Data Model(edmx:ConceptualModels) is created with designer and mappings specified. Database will be generated from this.
Code first: Create your .NET classes(no .edmx file). Database will be generated from this.
'Old school' would be using ADO.NET DataSet/DataTable.
Fastest data access is using DataReader and writing your own data access.
No framework will be faster than using a DataReader to populate your own POCOs.

EF 4.1 Code Only?

As far as I can tell (correct me if I'm wrong), there are two main approaches to using Entity Framework:
Model First: start with a predefined database and let EF create the code for you.
Code First: write the code, and let EF create the database for you.
I have an existing database and I'd like to write the code myself. Is this "Code Only" approach supported? Does such an approach even make sense in the context of EF?
I disagree with most of the other answers. From what I've seen, the EF "Code First" technology is really just a way to define your model using conventions, annotations, or a fluent mapping definition, rather than an EDMX file. If you write your "Code First" files to mirror your database schema, there is no reason that Entity Framework would be unable to produce the appropriate queries and statements using LINQ to Entities.
For more information, see Scott Guthrie's post on Using EF "Code First" with an existing database.
Entity Framework Power Tools allows you to reverse engineer a database to generate code first like code (that won't re-generate your database). Then you can tweak it from there as you need.
I believe you have to decide what your system of reference is -- the code (Code First) or the database (Model First). If you have an existing database, then go with a Code First approach, it will be hard to keep your changes in synch without generating your model from your code, or your code from your model.
If you have an existing database, but want to extend your model beyond the generated code, you could implement partial classes to accomplish this.
If you want to manually map your EF4 code and your database, you could consider this approach. However, this eliminates some of the benefit of an ORM, which is to set up the mapping for you.
Well I guess you can't have your cake and eat it too in this case - there has to be one definite source on what your model is, it is either the database (DB first), which then generates matching code for you, or the code (Code first) which will then create a matching DB.
The Entity Framework team answered these questions on their blog:
http://blogs.msdn.com/b/adonet/archive/2011/03/07/when-is-code-first-not-code-first.aspx

Categories