Entity Framework 4.1 Code First works great creating tables and relationships. Is it possible to create sql views or stored procedure using Code first approach? Any pointers regarding this will be highly appreciated. Thanks a lot!
相关问题
- How to Specify a Compound key in Entity Framework
- Using StoreGeneratedPattern.Identity with database
- Specify a cascading delete for parent / child rela
- Entity Framework 4.1 - Code First: many-to-many re
- Sort LINQ query by child collection's child pr
相关文章
- EF Codefirst validate unique property?
- Does AsQueryable() on ICollection really makes laz
- what is the best practice to set up DbContext in S
- How to add a column to a link table with code firs
- Implement WCF Data Service using the Repository Pa
- How to have different connection string for differ
- Entity Framework 4.1 - TPT Eager Loading - “The Re
- Why is MVC3 not scaffolding my foreign key columns
As Ladislav pointed out,
DbContext
in general does tend to minimize the logic in the database, but it is possible to execute custom SQL by usingcontext.Database.ExecuteSqlCommand()
orcontext.Database.SqlQuery()
.At first sight I really like the approach of Carl G but it involves a lot of manual interaction. In my scenario, I always drop all stored procedures, views... and recreate them whenever there is a change in the database. This way we are sure everything is up-to-date with the latest version.
Recreation happens by setting the following Initializer:
Then our seed method will get called whenever there is a migration ready
SQL Statements are stored in *.sql files for easy editing. Make sure your files have "Build Action" set to "Content" and "Copy to Output Directory" set to "Copy Always". We lookup the folders and execute all scripts inside. Don't forget to exclude "GO" statements in your SQL because they cannot be executed with ExecuteSqlCommand().
My current directory layout is as follows:
Now you just need to drop extra stored procedures in the folder and everything will get updated appropriately.
We support stored procedures in our Entity Framework Code First Migrations. Our approach is to create some folder to hold the .sql files (~/Sql/ for example). Create .sql files in the folder for both creating and dropping the stored procedure. E.g.
Create_sp_DoSomething.sql
andDrop_sp_DoSomething
. Because the SQL runs in a batch andCREATE PROCEDURE..
must be the first statement in a batch, make theCREATE PROCEDURE...
the first statement in the file. Also, don't putGO
after theDROP...
. Add a resources file to your project, if you don't have one already. Drag the .sql files from solution explorer into the Files view of the Resources designer. Now create an empty migration (Add-Migration SomethingMeaningful_sp_DoSomething
) and use:~/Sql/Create_sp_DoSomething.sql
~/Sql/Drop_sp_DoSomething.sql
emp's design works like a champion! I'm using his pattern but I also map stored procedures inside of my DbContext class which allows simply calling those context methods instead of using SqlQuery() and calling the procedures directly from my repository. As things can get a bit hairy when the application grows, I've created a check within my Seed method that makes sure the actual stored procedure parameter count match up to the parameter count on the mapping method. I've also updated the DROP loop emp mentioned. Instead of having to maintain a separate folder/file for the drop statements, I simply read the first line of each sql file and replace
CREATE
withDROP
(just make sure the first line is always justCREATE PROCEDURE ProcName
). This way all procedures in my StoredProcs folder get dropped and recreated each time Update-Database is ran. The drop is also wrapped in a try-catch block in case the procedure is new. For the procedure parameter count to work, you'll need to make sure you wrap aBEGIN/END
block around your tsql since each line of the file is read up to BEGIN. Also make sure each sp parameter is on new line.Enjoy!
EF code-first approach expects that there is no logic in the database. That means no stored procedures and no database views. Because of that code-first approach doesn't provide any mechanism to generate such constructs automatically for you. How could it do that if it means generating logic?
You must create them yourselves in custom database initializer by manual executing creation scripts. I don't think that this custom SQL constructs can be handled by SQL migrations.
It appears to be poorly documented however it appears you can now do some Stored Procedure manipulation using AlterStoredProcedure, CreateStoredProcedure, DropStoredProcedure, MoveStoredProcedure, RenameStoredProcedure in Entity Framework 6. I haven't tried them yet so can't yet give an example of how to use them.