I am developing an ASP.NET MVC 5 app with Entity Framework 6.0 in VS 2015 using a code-first approach. When I am adding a new model and migrate it, it's showing in DbContext and I am able to access it.
The problem is when I write a SQL query to create table (I don't need model for this because it is static data which is never going to be changed). After the update-database command, it's not showing up in context. If I create model and execute migrate command it is showing.
How to create a SQL Server table via query and access it in DbContext without creating models for it? Thanks in advance
Related
We made an app in C#; we used EF but have created the models using a database-first approach. We lost the database and need to recreate it in order to continue development; how do I use the model in my database-first C# app in order to automatically create the database in SQL Server (trying to avoid tedious database and table re-creation).
Thanks in advance,
Ronan
I have to use a synonym from external database in my application. Is it possible to scaffold a synonym using Entity Framework Core in database first approach? I've tried to pass a synonym name to -Tables switch while trying to scaffold, but an output in console told me "Unable to find a table in the database matching the selected table MY_SYNONYM." How to work with synonyms using EF Core?
In our circumstance we have replicated tables (using synonyms) to another DB from our ERP to lighten the load on the ERP for extra things like reporting. So I scaffolded the classes from the ERP itself and then pointed the context to the DB with the synonyms.
But to your question, 2 years later EF Core still does not appear support synonyms.
I have an existing database and I am operating on data inside it via a .NET C# application using ADO.NET currently. I am suggested to use Entity Framework model. However, I want to add some stored procedures and tables to the database and use them when the compiled application runs. Can I create entity-framework model for current database without these to-be-added stored procedures and tables and then add those during runtime when using the compiled application?
Is it possible with entity framework model? Can someone give me some pointers about how to do this? I am new to entity framework.
I am a student and beginner with Entity Frame Work Code First / DataBase First and MVC.
I am using the second one (DataBase First).
I created my database, generated the model from Database.
My question is that:
How can I initialize some default data (in the database)?
In code First pattern, there is a method called seed:
protected override void Seed(DatabaseContext context)
Is there an equivalent of this method using the DataBase First pattern?
In database first approach the database already exists so your application doesn't have any data initialization. Data initialization is used only when the application code creates the database (code-first) which is not your case.
You must simply put some data to your database either by using native SQL script or some database client tool like SQL Server Management studio or Visual Studio with Server Explorer.
Yes i did with Entity Framework Power Tools CTP1 which you can use via Extension Manager. After creating edmx model & installing tool right click on project and click "reverse engineer code first" and you'll get code first model of you database. Then you your db already have data you need to create new db from codefirst and export data from your previous db. If you are interesting in it i can show it by steps.
I am creating an application using Entity Framework code-first.
How can I create the database using this model? Is it possible to create the DDL scripts from a code-first approach just like the DDL scripts that can be generated from a model first approach?
Update : Generate a sql server script from Entity Framework code-first architecture
Using Entity Framework Code-First the application, when run, will create the database and tables that support your .Net model classes. Therefore using this approach you don't need DDL scripts as the work will be done for you by the application.
In Entity Framework there has been a lot of effort put into the Migrations tool to enable the deployment of db changes. This is detailed here:
Automatic Code First Migrations
Code First Migrations
This seems to offer the most effective way of deploying db changes to multiple target environments.
Code First is not particularly capable when it comes to local database deltas. If you are doing anything with OnModelCreating within your db context then this will require your database to be deleted then recreated by the code first application. This is required by code first to ensure that the code model and the persistence model are kept synchronised.
These are two good starter tutorials on MVC with an EF component.
Code-First Development with Entity Framework 4
Intro to ASP.NET MVC 3