Yuriy님의 프로필ygizhitsa사진블로그리스트 도구 도움말
    2월 4일

    Using Stored Procedure with ADO.NET Entity framework

    I think that Microsoft ADO.NET Entity framework (EF) has a big future which goes beyond just an ORM area. However, the first release is not perfect. Erik Kindblad discusses bunch of shortfalls of VS 2008 SP1 EF implementation. The biggest one is inability to map Entity to the stored procedure. My believe is that in the production environment the access to the tables has to be revoked and the only way to access the data in the table would be a stored procedure. I found several good articles aiming to resolve the same issue, but most of them require additional coding. Then I looked at edmx file to see what mapping xml has been produces by EF designer.

    <?xml version="1.0" encoding="utf-8"?>
    <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
    <!-- EF Runtime content -->
    <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="ClinetSODAModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    <EntitySet Name="Barrier" EntityType="ClinetSODAModel.Store.Barrier" store:Type="Tables" store:Schema="dbo" store:Name="Barrier">
    <DefiningQuery>
    SELECT
    [Barrier].[Barrier_CD] AS [Barrier_CD]
    ,[Barrier].[Description] AS [Description]
    ,[Barrier].[Last_Mod_Dt] AS [Last_Mod_Dt]
    ,[Barrier].[Last_Mod_ID] AS [Last_Mod_ID]
    FROM [dbo].[Barrier] AS [Barrier]
    </DefiningQuery>
    </EntitySet
     

    My first reaction was to put stored procedure call instead of the SELECT statement.

    <?xml version="1.0" encoding="utf-8"?>
    <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
    <!-- EF Runtime content -->
    <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="ClinetSODAModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    <EntitySet Name="Barrier" EntityType="ClinetSODAModel.Store.Barrier" store:Type="Tables" store:Schema="dbo" store:Name="Barrier">
    <DefiningQuery>
    EXEC [dbo].[sp_GetAllBarrier]
    </DefiningQuery>
    </EntitySet>

    The T-SQL generated by EF run-time failed, but gave me a material to play with

    SELECT
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
    LEFT OUTER JOIN (SELECT
        COUNT(cast(1 as bit)) AS [A1]
        FROM (
    EXEC [dbo].[sp_GetAllBarrier]
    ) AS [Extent1] ) AS [GroupBy1] ON 1 = 1

     

    Finally, I found a construct that would let me to use stored procedure.

    <?xml version="1.0" encoding="utf-8"?>
    <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
    <!-- EF Runtime content -->
    <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="ClinetSODAModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    <EntityContainer Name="ClinetSODAModelStoreContainer">
    <EntitySet Name="Barrier" EntityType="ClinetSODAModel.Store.Barrier" store:Type="Tables" store:Schema="dbo" store:Name="Barrier">
    <DefiningQuery>
    SELECT * FROM OPENQUERY(LOCALSERVER, 'EXEC SODA.dbo.sp_GetAllBarrier')
    </DefiningQuery>
    </EntitySet>
     

    Yes, it would require me to register local server

    sp_addlinkedserver @server = 'LOCALSERVER', @srvproduct = '',
    @provider = 'SQLOLEDB', @datasrc = @@servername

     

    And the usage of full stored procedure name including DB name is not convenient, but step forward and buys some time before Microsoft issues new EF release.

    댓글

    잠시만 기다려 주세요...
    죄송합니다. 입력한 댓글이 너무 깁니다. 내용을 줄여 보세요.
    입력한 내용이 없습니다. 다시 시도해 보세요.
    죄송합니다. 지금은 댓글을 추가할 수 없습니다. 나중에 다시 시도해 보세요.
    댓글을 추가하려면 부모님의 사용 허락이 필요합니다. 허용 요청
    부모님이 댓글 기능을 해제한 상태입니다.
    죄송합니다. 지금은 댓글을 삭제할 수 없습니다. 나중에 다시 시도해 보세요.
    하루에 남길 수 있는 댓글의 최대 한도를 초과했습니다. 24시간 후에 다시 시도해 보세요.
    회원님의 계정은 다른 사용자에게 스팸 메일을 보낼 수 있다고 여겨지므로 댓글 기능이 비활성화되어 있습니다. 이 설정에 문제가 있다고 생각되면 Windows Live 지원에 문의하시기 바랍니다.
    댓글을 남기려면 아래 보안 검사를 완료해야 합니다.
    보안 검사에 입력한 글자는 그림 또는 오디오에 있는 글자와 일치해야 합니다.

    댓글을 추가하려면 Windows Live ID로 로그인하세요. 핫메일, 메신저 또는 Xbox LIVE를 사용하는 경우 해당 계정을 Windows Live ID로 사용할 수 있습니다.로그인


    Windows Live ID가 없으신가요? 등록

    트랙백

    이 블로그의 트랙백 URL은 다음과 같습니다.
    http://ygizhitsa.spaces.live.com/blog/cns!8A7B4991A271531A!203.trak
    이 블로그를 참조하는 웹 로그
    • 없음