How do I use Entity Framework in a CLR stored procedure?

落花浮王杯 提交于 2019-12-06 08:38:15

问题


I am looking forward to move all the logic (which is implemented as manipulating Entity Framework 4 objects) to a server side. It looks going to be simple (thanks to the application structure) and beneficial (as all I have is one oldy laptop as a client and one tough server which runs SQL Server 2008, and building a separate service for the logic can just introduce more latency if compared to doing it inside the database).

So how do I correctly use Entities Framework inside a CLR stored procedure and make it using a host-server-provided SqlContext?

Suddenly I couldn't find any examples in the Web. Looks stragne that none has yet dine this. Does it mean that the task is ridiculous and I definitely should not do that? It won't be very convenient to use T-SQL and access tables directly instead of EF classes because my model makes heavy use of inheritance and has a very complex table-per-type structure of lots of very simple tables.


回答1:


No, you can't. Visual Studio wouldn't even let you add the file or project type. (That's too bad, I wanted to do that also to process some really complex logic.)




回答2:


You cannot - at least not at this time. The CLR contained in SQL Server 2005 through 2008 R2 is the .NET 2.0 CLR - and Entity Framework 4 requires the .NET 4 framework.

So for now, when doing stuff inside a SQL-CLR method, you're limited to straight ADO.NET 2.0 only.

The bigger question then remains: why on earth would you want to use EF4 inside a SQL-CLR function? Those are intended to be stored proc, user-defined functions, user-defined aggregates - but certainly not full-blown database apps, really...




回答3:


Some of us are not that keen to maintain database schema metadata in two or more places. EF is great to do this, but if one wants to employ the performance benefits of SQL-CLR the schema has to be defined in its context. Which at the moment means a custom metadata codes in SQL-CLR assembly that generates DDL to define a database and import it to EF.




回答4:


Seems that with SQL Server 2008 R2 or greater you can, it uses .NET ver. 4.0. See this blog.

I run the following command against my SQL Server 2010 Developer Eeition:

select value from sys.dm_clr_properties where name = 'version'

and got the following output:

v4.0.30319



来源:https://stackoverflow.com/questions/2793363/how-do-i-use-entity-framework-in-a-clr-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!