Reputation: 573
I am using SQL Server 2012 RC0 + EF 4 + MVC 4. 90% of my db is made with Code First funcionality of EF.
I am going to use some CLR functions for regular expressions, as described here and other articles.
As I stated, I am using EF Code First, so I take advantage of this funcionality, creating db on first use and executing some additional SQL commands for making indexing, constraints, SQL computed fields and SQL functions.
This is an example of my Seed funcion on first use:
public class mydbInitializer : DropCreateDatabaseIfModelChanges<TManagerContext>
{
protected override void Seed(mydbContext context)
{
try
{
context.Database.ExecuteSqlCommand("CREATE NONCLUSTERED INDEX IX_index1 ON table1 (field1)");
context.Database.ExecuteSqlCommand("CREATE NONCLUSTERED INDEX IX_index2 ON table1 (field2)");
...
// here I would put some code to Enable CLR on SQL Server and deploy my CLR functions into it....
I would deploy my app to different server, so I was wondering if it was possible to enable CLR and deploy CLR functions at first use of the application? For safety reasons, then I would go to replace the bins with versions that do not involve the creation or drop the db.
Do you think it is a wrong approach? Should I necessarly need to use SQL Management Studio? Alternatively, you can create batch to facilitate the integration of the CLR and its bins?
Thank you very much for your help and sorry for my bad English. Best Regards
Upvotes: 0
Views: 688
Reputation: 294487
Yes, it is possible. Enabling SQLCLR can be done by running sp_configure
:
exec sp_configure 'clr_enabled', 1;
reconfigure;
To load an assembly into the database straight from code use CREATE ASSEMBLY <name> FROM @bits;
and as as @bits pass in the assembly DLL stream. You can add the SQLCLR assembly to your application as a resource or as a dependency, both allow you to retrieve a stream to the assembly DLL and pass this in as a SqlBytes
argument for @bits.
Upvotes: 3