Importing an Entity Framework function from code

entity-framework

Question

I've created a stored procedure in my database named Spr EventLogCreate. I have created a function import in my data model called LogEvent with no return type, and I can see this function in the Model Browser tree at

EntityContainer > Function Imports > LogEvent in MyModel.edmx > MyModel

I reasoned that I should therefore be able to use the following code to call the function:

var context = new MyModelEntities();
context.LogEvent(...);

However, the LogEvent() function is absent.

How can I invoke my imported function? I must be quite ignorant.

use EF 3.5 and VS 2008.

1
3
3/9/2010 2:48:18 AM

Accepted Answer

In the default Microsoft designer, there is no method to build code that calls a function without returning a result set from design time. You may create code that manually invokes it in your context class's partial declaration. Here's an illustration:


        public void EmpDelete (global::System.Nullable PEMPNO, global::System.Nullable PDEPTNO)
        {
            if (this.Connection.State != System.Data.ConnectionState.Open)
              this.Connection.Open();
            System.Data.EntityClient.EntityCommand command = new System.Data.EntityClient.EntityCommand();
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = @"DataSourceModel1Entities.EmpDelete";
            command.Connection = (System.Data.EntityClient.EntityConnection)this.Connection;
            EntityParameter PEMPNOParameter = new EntityParameter("PEMPNO", System.Data.DbType.Decimal);
            if (PEMPNO.HasValue)
                PEMPNOParameter.Value = PEMPNO;
            command.Parameters.Add(PEMPNOParameter);
            EntityParameter PDEPTNOParameter = new EntityParameter("PDEPTNO", System.Data.DbType.Decimal);
            if (PDEPTNO.HasValue)
                PDEPTNOParameter.Value = PDEPTNO;
            command.Parameters.Add(PDEPTNOParameter);
            command.ExecuteNonQuery();
        }
2
3/11/2010 3:32:14 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow