如何在代码隐藏中设置EntityDataSource的Where子句

时间:2011-07-21 22:41:40

标签: c# .net entity-framework

        PaymentsDueEntityDataSource.ContextTypeName = "Bills.DAL.BillsEntities";
        PaymentsDueEntityDataSource.EnableFlattening = false;
        PaymentsDueEntityDataSource.EntitySetName = "tblPayments_Due";

        PaymentsDueEntityDataSource.Where = "it.UserName = " + HttpContext.Current.User.Identity.Name.ToString();

        PaymentsDueEntityDataSource.Include = "tblType, tblRepeat";
        PaymentsDueEntityDataSource.EnableUpdate = true;

当我删除Where子句时,我的gridview返回所有记录。当我硬编码从HTML中的HttpContexxt字符串生成的相同字符串时,我的gridview返回正确的记录。但是,当我尝试使用上面的代码时,我收到了超出范围的消息:

'kwingat'无法在当前范围或上下文中解析。确保所有引用的变量都在范围内,加载了所需的模式,并正确引用了名称空间。近简单标识符,第6行,第15列。

任何想法?

1 个答案:

答案 0 :(得分:2)

我认为您需要将其作为参数传递

PaymentsDueEntityDataSource.Where = "it.UserName = @UserID";
PaymentsDueEntityDataSource.WhereParameters.Add(new Parameter("UserID", TypeCode.Int32,  + HttpContext.Current.User.Identity.Name.ToString()));