views:

285

answers:

1

So I have a stored procedure called Spr_EventLogCreate defined in my database. 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

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

I thought I should then be able to call the function in my code as follows:

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

But the LogEvent() method is not present.

I must be being really stupid here, but how do I call my imported function?

Using VS 2008 and EF 3.5.

A: 

There is no way to generate the code calling the function not retuning a result set from design time in default Microsoft designer. You can write code calling it manually in the partial definition of your context class. Here is an example:


        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();
        }
Devart
Hm thanks... any idea why it is implemented this way? Why can it not generate the calling code automatically?
Mikey Cee
It is a problem of VS 2008. The code is generated in VS 2010 RC.
Devart