EF SingleOrDefault不适用于ASP.NET

时间:2011-04-14 07:24:41

标签: asp.net unit-testing linq-to-entities

我为我的项目编写了一些单元测试,测试我的演示者,这些演示者使用SingleOrDefault查询EF上下文,所有单元测试都成功。但是,当我运行我的ASP.NET应用程序时,我不断得到Linq to Entities不支持的错误“'Single”?我想知道为什么这种行为会被踢进去?我找不到有关此行为的任何文档。

我使用以下代码:

主讲人:

public class ManagedQueryPresenter : BasePresenterMetModel<IManagedQueriesView, ManagedQueryBeheerModel>, IWebPartPresenter
    {
        public ManagedQueryPresenter(IManagedQueriesView view) : base(view, new ManagedQueryBeheerModel()) { }

        #region IPagePresenter Members

        public void OnViewInitialize()
        {

        }

        public void OnViewInitialized()
        {

        }

        public void OnViewLoaded()
        {

        }

        #endregion

        public void OnManagedQueriesSelecting()
        {
            View.ManagedQueries = Model.GetAll();
        }

        public void OnManagedQueryInserted(ManagedQuery entity)
        {
            Model.AddManagedQuery(entity);
            View.ManagedQueries = Model.GetAll();
        }

        public void OnManagedQueryUpdated(ManagedQuery entity)
        {
            Model.UpdateManagedQuery(entity);
        }

        public void OnManagedQueryDeleted(ManagedQuery entity)
        {
            Model.DeleteManagedQuery(entity);
        }
    }

型号:

public class ManagedQueryBeheerModel : BaseModel, IModel
{
    public void AddManagedQuery(ManagedQuery entity)
    {
        ...
    }

    public void DeleteManagedQuery(ManagedQuery entity)
    {
        ...
    }

    public void UpdateManagedQuery(ManagedQuery entity)
    {
        DoEntityAction<bool>(context =>
        {
            ManagedQuery toUpdate = context.ManagedQueries.Include(q => q.ManagedQueryParameters).SingleOrDefault(x => x.ID == entity.ID);

            ...

            context.SaveChanges();

            return true;
        });
    }

    public IList<ManagedQuery> GetAll()
    {
        return DoRepositoryAction<ManagedQuery, List<ManagedQuery>>(repository => (List<ManagedQuery>)repository.GetAll());
    }

    public ManagedQuery Get(long ID)
    {
        return DoRepositoryAction<ManagedQuery, ManagedQuery>(repository => repository.GetSingleOrDefault(x => x.ID == ID));
    }
}

单元测试:

    [TestMethod()]
    public void OnManagedQueryUpdatedTest()
    {
        IManagedQueriesView view = new MockedManagedQueriesView(); 
        ManagedQueryPresenter target = new ManagedQueryPresenter(view);
        ManagedQuery entity = ManagedQueryHelper.CreateNewRecord(target.Model);
        entity.Name += "Updated";
        target.OnManagedQueryUpdated(entity);
    }

public static class ManagedQueryHelper
    {
        public static ManagedQuery CreateNewRecord(ManagedQueryBeheerModel model)
        {
            ManagedQuery entity = new ManagedQuery
            {
                Description = "Test Query",
                Name = "Test",
                QueryText = @"SOME QUERY",
                HasOutput = true,
                Category = "Test",
            };
            model.AddManagedQuery(entity);
            return entity;
        }
    }

ASP.NET视图(ascx的代码隐藏):

public partial class ManagedQueriesUserControl : WebPartMangedUserControlWithPresenter<ManagedQueryPresenter>, IManagedQueriesView
    {
        protected ASPxGridView _grid;
        protected ObjectContainerDataSource _ocdsManagedQueries;

        #region IServicesView Members

        public IList<Entities.ManagedQuery> ManagedQueries
        {
            set 
            {
                _grid.ForceDataRowType(typeof(ManagedQuery));
                _ocdsManagedQueries.DataSource = value; 
            }
        }

        #endregion

        protected void _ocdsManagedQueries_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
        {
            Presenter.OnManagedQueryDeleted((ManagedQuery)e.Instance);
        }

        protected void _ocdsManagedQueries_Updated(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
        {
            Presenter.OnManagedQueryUpdated((ManagedQuery)e.Instance);
        }

        protected void _ocdsManagedQueries_Inserted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
        {
            Presenter.OnManagedQueryInserted((ManagedQuery)e.Instance);
        }

        protected void _ocdsManagedQueries_Selecting(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceSelectingEventArgs e)
        {
            Presenter.OnManagedQueriesSelecting();
        }

        #region IWebPartView Members

        public bool IsValid()
        {
            return Page.IsValid;
        }

        public string ErrorText
        {
            set { }
        }

        #endregion
    }

2 个答案:

答案 0 :(得分:1)

我相信你可以在这个帖子Error, method not supported by LINQ to Entities

找到答案

答案 1 :(得分:0)

我忘记了在.NET 4中编写的VS2010和.NET3.5中的代码默认使用单元测试,因此无法正常工作。在EF4 Single(OrDefault)支持!