ObjectContext Query()上的SQL错误无效 - >执行()

时间:2014-05-19 14:30:27

标签: c# entity-framework objectcontext

我正在尝试从覆盖的SaveChanges()内部更新表。

此查询:

UPDATE NewClub SET LastActivityDate='5/19/2014 10:17:50 AM' WHERE Id=21

获取此错误

The query syntax is not valid. Near identifier 'NewClub', line 1, column 8.

显然,我不能在这里使用直接SQL,因为SQL是有效的。查询应该是什么样的,或者我错过了其他什么?

由于

     ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;

     List<ObjectStateEntry> objectStateEntryList =
           ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
                                                      | EntityState.Unchanged
                                                      | EntityState.Modified
                                                      | EntityState.Deleted).ToList();

     foreach (ObjectStateEntry entry in objectStateEntryList)
     {

            if (newClubPrimaryKeyId > 0)
            {
                string q = @"UPDATE NewClub SET LastActivityDate='" + DateTime.Now + "' WHERE Id=@id";
                ObjectQuery<NewClub> newClubQuery = ctx.CreateQuery<NewClub>(q, new ObjectParameter("id",newClubPrimaryKeyId));
                newClubQuery.Execute(MergeOption.PreserveChanges);
            }
    }

1 个答案:

答案 0 :(得分:0)

我通过更改查询执行语句解决了这个问题:

            if (newClubPrimaryKeyId > 0)
            {
                string q = @"UPDATE NewClub SET LastActivityDate='" + DateTime.Now + "' WHERE Id="+newClubPrimaryKeyId;


                using (var context = new ReportingContext())
                {
                     var result = context.Database.ExecuteSqlCommand(q);
                }


            }