Entity Framework ObjectContext -> raw SQL calls to native DBMS

前端 未结 4 1941
旧时难觅i
旧时难觅i 2021-01-13 19:52

I have an app using the ADO.NET entity framework (the VS2008 version, not the newer, cooler one) and I need to be able to make a call down to the underlying DBMS (it\'s post

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 20:30

    According to this post there is no support of DML in Entity Framework V1.

    But there is support of stored procedures, but only such of them that return entities. That means if you create stored procedure in that way (sql server syntax):

    CREATE PROCEDURE [dbo].[usp_trncate]
    AS
    BEGIN
     truncate table t1
    END
    

    You can't import it as function (actually you can, but it won't work - no code for this function will be generated)

    I have found crutch solution to achive the goal: if you define sp as below:

    CREATE PROCEDURE [dbo].[usp_trncate]
    AS
    BEGIN
     truncate table t1
    
     select top 1 * from t1
    END
    

    you can import as function and use it in your code like this:

    TestEntities context = new TestEntities();
    context.TruncateTable();
    

    (TruncateTable is the name of imported function)

    I think there is another (better) solution. Other ways to digg are to tinker with .edmx file to make sp without return works or write another sp and map it to table insert function (that sp must truncate table and insert row)

    I hope it will help you.

提交回复
热议问题