Задайте вопросЗадайте вопрос
 

ОтвеченоProblem using stored procedures returning a CLR type

  • 7 января 2008 г. 18:04Ken Lefler Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     

    Hi,

     

    I have a problem using stored procedures in the Entity Framework. I have a stored procedure that returns XML generated with the "For XML" syntax, so the procedure does not return an Entity, but a string. I'm new to this, so I hope someone can show me a step I am missing.

     

    I have VS 2008 RTM, ADO.Net Entity Framework Version 1.0 (beta 3), and ADO.Net Entity Framework Tools Preview installed. I'm following the procedure outlined in Noam Ben-Ami's video, namely:

     

    1) Add a new item of type ADO.Net Entity Data Model to the project, choose Generate from Database in the wizard, connect to my database, and select no entity tables, but only my stored procedure. My model designer workspace is empty.

    2) I then select my stored procedure under the Model.Store in the Model Browser and select the "Create Function Import" menu item, give it a name and select "String" as the return type. Note I am selecting a CLR type, not an Entity. My imported function now appears under the Function Imports section under the EntityContainer.

    3) I Build the project successfuly. When I look at the C# code behind file there are only entity constructors, no imported stored procedure. If I try to use it anyways, Intellisense is unaware of it and the compile complains about a missing extension method. There must be some step I'm missing here.

    4) For the fun of it, if I add an Entity table and change the return type to this entity (rather than string), then the imported function does appear in the code behind file and I could compile (but of course it won't work right). In other words if the return type is an entity the stored procedure import works, but not if it is a CLR type.

     

    Thanks for your help.

Ответы

  • 7 января 2008 г. 23:34Noam Ben-Ami - MSFTMSFT, МодераторМедали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     Отвечено

    Unfortunately, due to time constraints, we do not do codegen for stored procedures that return primitive types such as String/Int32, etc.

     

    For now, you can create your object context and get its connection. Then, use the connection's "CreateCommand" method, and create a command of type stored procedure whose name is the name of the function import. Then you can execute that command and get your string back. E.g. if your function is called "getXMLAsString" then in your DataContext's partial class you would write:

     

    System.Data.Common.DbConnection connection = this.Connection;

    System.Data.Common.DbCommand command = connection.CreateCommand();

    command.CommandType = System.Data.CommandType.StoredProcedure;

    command.CommandText = "getXMLAsString";

    string result = command.ExecuteScalar() as string;

     

    We will make this experience better in the future.

     

    HTH,

      Noam

Все ответы