Is there a native Entity Framework way to create a database (LocalDB) without populating it with the tables defined in my DbContext class (code first)? (I want to use my own database creation script, with default values, etc, and I want to be able to clear it out beforehand using context.Database.Delete().)
If I use context.Database.Create() then EF will populate the database with tables. If not then no database exists and I cannot connect to it to run my database creation SQL.
I am providing a connection string like this:
data source=(LocalDB)\\MSSQLLocalDB;integrated security=True;attachdbfilename=|DataDirectory|\\FileName.mdf;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework
Create a base DbContext instance that uses the same database, and use it to do the recreation.
var my_context = new MyDbContext(connection_string);
var creater_context = new DbContext(connection_string);
creater_context.Database.Delete();
creater_context.Database.Create();
// now use my_context on the database.
Related
I am working on a project in which I am assigned to implement database first Approach. Here, I want to know that when we initiate database first approach we map that to an existing DB, but what if I have another DB with the same structure but different data, can I use that DB by just changing the connection string ? or will it impact somehow?
It will work when you change the connection string. I recommend you select the 'Code First from database' when creating new 'ADO.NET Entity Data Model' with VS add new item.
I have 5 tables to use in one mvc view. So I created a database view and want to map this view into EF for using it as a model. How can I do?
I have successfully used the following in a code-first application where I used Sql Server Functions as 'views' to run complex queries against the database to fetch data (I did not need inserts or updates).
List<MyClass> myClassItems =
dbContext.Database.SqlQuery<MyClass>("Select * from dbo.MyClassFunction(#param)", param).ToList();
I returned myClassItems as the result of a Repository function, with MyClass a class with properties that match the columns that the MyClassFunction returned.
I have an entity (Org) in my Entity Framework that has a foreign key with a table that is in another database (BusinessUnit). We need the foreign key to get the description of the BusinessUnit linked with the Org. In the past (old project without Entity Framework) we were using a stored procedure to return all the information about this entity, including the BusinessUnit description, using a join. So now my problem is how to display the same information than before using Entity Framework.
I've tried, once I load my Org entity from database, to make a loop accessing to BusinessUnit to get the description for each Org, but this is too slow. My other idea was use a store procedure, but I need an extra field on my entity and Entity Frameworks gives me an 3004 error: No mapping specified for my property. I was thinking to use a complex type, but I'm not sure if it's what I need keeping in mind I have to add just a field to my entity. In that case, could I use the complex type just for "select" operations and keep my original entity for the rest of CRUD operations?
How should I proceed?
Thanks.
EF is not able to execute queries across multiple databases. If you need to perform such query you must either use database view and map the view as a new entity (it will be readonly - making it updatable requires mapping insert, update and delete stored procedures) or divide your data querying into two separate parts to load data from both databases. Divided querying can either use two contexts or you can get data from the second database using stored procedure.
The reason why you got the error is that you added the property in EDMX. EDMX can contain only properties mapped to your first database. EDMX generates entity code as partial classes If you need property manually populated from the second database you have to create your partial part (partial class) for entity and add the property in code.
For integration testing I need to wipe all data information in a database.
I'm using Entity Framework 4
What's the most efficient way?
Would be nice if I don't need to specify table or class names every time I add a new class to the model.
Thanks!
Most efficient way to delete database data is to drop all tables and recreate all tables.
This may not be the fastest, but it is the simplest. It even clears your connection pool:
using (var context = new DataContext())
{
context.Database.Delete();
}
I have a single hosted SQL Server DB and I don't have permissions to drop it. How do I make EF create tables from my domain classes? RecreateDatabaseIfModelChanges and AlwaysRecreateDatabase try to drop DB, CreateDatabaseOnlyIfNotExists doesn't create the tables.
Thx
CreateDatabaseOnlyIfNotExists is the default strategy. It means you don't need to even set it through the Database.SetInitializer. EF Code First will check the database and if couldn't find one with the same name as your context's fully qualified name, it will create one for you.