I have been using ADO.NET Entity Data Model Ef 6.x database first with different MS SQL Server databases and it has been working alright until I've got a prod local copy of production database schema having a lot of tables. I'm not doing anything fancy rather just trying to add DbContext with the standard wizard. For some reasons, it is taking ages and never successfully creates DbContext and entity models. It takes insane amount of time even when I try to select one single table out of whole lot of tables but it at least successfully creates DbContext. I need to create DbContext for pretty much every table within the database. Any thoughts how can I generate that using ADO.NET entity data model EF 6.x?
Following SQL script resolved my problem.
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION=ON
Once you generate DbContext running the above script you should turn that back off
with the following script
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION=OFF
Related
I have 2 web applications which are let's say Legacy1 and New1. I have separate DbContext for both applications and separate databases.
Now I want to migrate data from the Legacy1 database to the New1 database. I cannot simply use copy database or export database options in SQL Server. Entities are different and I need to incorporate some logic while migrating data.
I have decided to use .Net Core Console (.Net Framework) project to do this. Do I have to add both DbContexts to this project? Also all the entities which I want to map and do the migration or is there any other way to achieve this.
Finally i found a simple approach to achieve that. I used EF DB First approach to generate edmx files for source and target databases. Which gives me access to corresponding Entities as well.
I Queried source database using source dbcontext and manipulated data as per the requirements and inserted into target database using target dbcontext. I used AutoMapper to map source entities to Target entities.
It was much simpler than i thought earlier. Hope it helps others having similar scenario.
I am having an issue with my website(ASP.NET, c#, SQL, Code-first Entity Framework).
I have a project with an attached SQL database generated from code first entity framework.
Now I have imported another SQL database using model first database which looks almost same but the table and column names are different.
So now I would like to write data to two databases at the same time with just one click from my web application.
The newly attached database will be a backup and we should write data to both databases at the same time.
Any suggestions would be highly appreciated.
Thanks
As others have suggested, you need to do the mapping yourself, but one thing I would like to add, you may need to wrap your SaveChanges() into a transaction, you may find steps here: https://msdn.microsoft.com/en-us/data/dn456843.aspx
I'm following the Entity Framework tutorial on:
Link
I've downloaded the source code, and ran. The project works fine (using the default connection string).
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0" />
Next i've changed the connection string to connect to a remote server (which successfully connects). However the tables aren't created and when running the application and accessing the controller I get the following error.
Error:
Model compatibility cannot be checked because the database does not contain
model metadata. Model compatibility can only be checked for databases created
using Code First or Code First Migrations.
My database user is 'dbowner' so I wouldn't imagine it's database access issues.
I'm new to EF, and don't know much about Code First Migrations. Have you come across the above error, and would Code Migrations solve this issue? If so why?
From my reading (please correct me if I am wrong) the scenario here is that you have an existing (perhaps empty) database on a remote server that you wish to put your EF code-first work into.
The error is coming about because, I think, EF is looking for a _MigrationHistory table (metadata about what EF code-first migrations have been run against it etc) and can't find it. There is some reasonable coverage of this table here for some background/interest reading.
So to resolve the issue, I think the steps to take are:
Initialise your database so that it acknowledges the EF code-first stuff
Update the database, rolling forward all your migrations to date
I found some good coverage of how to do this here. This blog also has some good coverage of EF topics in general
PS. I am guessing that your .dsf/SQL Compact Edition database wouldn't have had this problem because EF code-first would have created it for you on first run so it was already acknowledged as being a code-first database.
Here is a link to Entity Framework Power Tools. It is made for creating models by 'Reverse Engineering' your Database on a remote server. You can then easily access this database using EF.
Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database
Both of the initializer methods which I had tried fail when the database already exists:
Database.SetInitializer<Context>(new Initializer());
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Context>());
However it is possible to force the database to be dropped using:
Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
The following SO post provides the answer to my question:
Setting up a Entity Framework Code First Database on SQL Server 2008
I've tried a combination of the two, and have decided that the best solution is to manually go into SQL Management studio and DROP the database, and re-create it using the initializer as this allows me to Seed the contents of the database.
Database.SetInitializer<Context>(new Initializer());
See here for more information on Seeding the database as it is also quite an unstable processess!
How can I get my database to seed using Entity Framework CodeFirst?
In SQL server I renamed my table TrialOrderStockNumbers to TrialOrderStock. A foreign key points back to the TrialOrders table. In the EF designer I deleted the TrialOrderStockNumbers entity and ran the Update Model From Database wizard (adding back the table TrialOrderStock). When I run the application the child data is now missing.
Now in SQL Profiler no query executes to get the Stock data. Before making the change profiler showed a statement selecting the data. I have had to discard all my changes and (for now) I plan to leave the old table name.
I'm using Entity Framework database first. Why did Entity Framework stop retrieving child data after renaming the table in SQL Server and updating the model?
It has been a while since I worked with MEF, where I had a similar issue with the entities getting out of sync with the database schema. As far as I recall, I simply re-generated the domain service and the problem was resolved.
I started a project and use Entity Framework 5.
Now I created a database on my SQL Server Express with all tables.
Further I created the DbContext with a fluent mapping.
What is better, the fluent mappings or the .edmx mapping files?
The database is now on the SQL Server but I want support also SQL local db.
Is there a way to tell the EF that I want to use a SQL local db?
Should I ship the whole database within the setup or better to create the database on startup of my application? How can I use EF to create the database (SQL server or SQL local db)?
Every useful help would be appreciated.
If you are using EF5 I would stay away from edmx.
You could reverse engineer your model from database using Entity Framework Power Tools.
Than you can customize your model using either Data Annotations or fluent mappings.
If you use EF5 code first it can create database automatically for you if not present, however that would not work very well on subsequent upgrades (it can recreate database but then you will use or your existing data). The options you could use, is either EF migrations, where you can specify in fluent-like languagage the modifications that were made to your database or use Database Project in Visual Studio, where you can store all your schema in source control and then generate database upgrade scripts betweeen releases.