How to use Entity Framework 4 against system views on SQL Azure?
I'm a newbie to Entity Framework, so I'd like to try something simple to get started on Visual Studio 2010.
Suppose I start out with the following SQL statement (the View sys.databases exists in the master database on SQL Azure):
SELECT name, create_date
FROM sys.databases
WHERE database_id > 3
So instead of us开发者_开发知识库ing traditional ADO.NET of executing this SQL and using a DataReader to insert the results into a custom class, I attempt doing things the EF way.
So I attempted to create a Entity Data Model. In the Wizard, I am given two choices: "Generate from Database" or "Empty Model". I tried to "Generate from Database", but the Wizard doesn't list the vast majority of the Views in the master database. So that approach appears to be a dead-end. I then tried "Empty Model", which is a bit more work (for this newbie) and insists on prefixing "databases" with "dbo", and I need "sys". So I'm basically stuck, and I haven't found any examples of people using EF to work with system views.
Does anyone have any detailed examples of accomplishing this? Many thanks.
Create a new view which which returns the system view that you want, and then use that in your EDMX design.
For example:
CREATE VIEW [dbo].[SysDatabasesView] AS SELECT * FROM sys.databases
then use your SysDatabasesView
instead of sys.databases
.
if the goal is to use EF datacontext, create a complex type 'DB' in EF that contain name and create_date.
Issue the query directly using ExecuteStoreQuery
static readonly String dbCommand = @"select [name],create_date from sys.databases where id > 0";
context.ExecuteStoreQuery<DB>(dbCommand);
Well... You just need to add Master DB on EDMX and import the system view sys.databases.
精彩评论