I am using CodeFirst approach, EF 4.0. I have model of tables that application uses. However I want to use another database as encyclopedia, meaning this database has a lot of data and these data is interconnected by foreign key. I have declared DbSet of my external datebase. However when model is generated, all entities that has to exist within my external DB, are placed as empty tables in my main DB. So here is the question. How can I specify for some entities to use external DB while for other use main DB?
Related
I have been asked to write a web site that will use an existing SQL Server database. The database was designed to work with another application, and so I can't make any potentially breaking changes to it.
Unfortunately, the database does not contain a single relational link, each table is standalone.
I know you can use EF commands to scaffold a database and create entity classes, but I would like the code to know the relationships that should exist between the tables.
I thought about scaffolding the database, then modifying the created classes to include the links, but I'm not sure if that would allow EF to load related entities. Also, as I will need to add some new tables to the database, I'm worried that EF will try and create those links when I do the migration.
Is there any way to do this?
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
I'm using Entity Framework 6 Code-First and my application is in production. I have to perform some changes in my models without lose any data.
I have to perform changes like these:
Change a relationship of two entities, from one-to-many to many-to-many.
Change an entity property's data type.
I need something like the Seed Database, but I need to load the data before change the model to later insert it again... I need to move the data. When I change the relationship I don't lose the data but the relation do.
How can I do this process?
Thanks so much for any help.
I would suggest you follow these steps:
create temporary tables that represent the data & relationships of the affected tables. Ensure you do this outside of EF, using CREATE TABLE sql
BEFORE running Update-Database, run a script to move data from the tables in question to your temp tables
Run Update-Database
Run script to insert data and relationships back into your new tables
Drop temp tables
I generated model from existing database in c# via entity framework, but when i change database (for example add column to table) application stop work, because in model miss the new column, but i donĀ“t need the new column. Is there any solution to generate model from database, but do not be independent to change in db?
Thanks.
I'm pretty new to Entity Framework: I started from a database-first model to maintain an application created using a strange mixture of EF and plain old SQL.
I created my own fresh DB-first model and I'm fine with it. Today my boss asked me to add a new entity. Lack of foreign keys simplifies the scenario.
I have created my new entity in the diagram (it's made of three instances of a Complex Entity I just created) but now I have to make an incremental DB script to create the new table. I'm supposed to do that both for MySQL and SQL Server but let's start with the second.
So now I see that I have a compilation problem "No mapping for entity Entity" and if I use "Update model from database" command I see no option for pushing changes to DB, but that sounds correct given the word "from".
OK, I have tried to click "Table Mapping" from the right-click menu and I found the option to map the entity to the table. I was going to type the new table name in the "Add table or view" field and... WAIT! I can only select existing tables
I understand it's just for a single table so I can simply "Generate database from model" in order to get the full SQL script, find the table I want, run that to DB and "Update model from DB" so EF will see the table, BUT
I would like to understand how to create incremental scripts with Entity Framework. That is my question.
You indicate you have a database-first design but appear to be working from a code-first mindset.
In database-first design the entity model is subordinate the underlying datastore. Changes to the model (or at least changes to the model which also require changes to the underlying datastore) occur FIRST on the database.
So how do you create a new table for the entity? You create the new table in your database (CREATE TABLE ...). Then using the "Update Model From Database" wizard you select the new table from the "Add" tab. EF will create the corresponding EF class automatically. If you already manually created the entity you should delete it otherwise you could end up with some weird entity naming (i.e. Customer1).
Database first does not have the capability to support table creation at the entity layer. Changes to the database are always one way, from the database to the entity model, hence the term "database first".
On the other hand if you are more comfortable creating entities directly and want to build a database from a set of entities you should be looking to create a "Code-First" design. Despite the name "code first" it is possible to get an initial set of entity classes from an existing database. The term "code first" refers to the origination of changes to the db/model structure.