从代码调用实体框架函数导入

时间:2010-03-09 02:48:19

标签: entity-framework

所以我在我的数据库中定义了一个名为Spr_EventLogCreate的存储过程。我在我的数据模型中创建了一个函数import,名为LogEvent,没有返回类型,我可以在模型浏览器树中看到这个函数

MyModel.edmx> MyModel> EntityContainer>功能导入>的LogEvent。

我认为我应该能够在我的代码中调用该函数,如下所示:

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

但LogEvent()方法不存在。

我在这里一定非常愚蠢,但如何调用导入的函数?

使用VS 2008和EF 3.5。

1 个答案:

答案 0 :(得分:2)

在默认的Microsoft设计器中,无法生成调用函数的代码,而不是从设计时重新调整结果集。您可以编写在上下文类的部分定义中手动调用它的代码。 这是一个例子:


        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();
        }