开发者

Value cannot be null; Parameter Name: constructor when using SqlExecuteQuery with an abstract Entity model

We are storing items in our database which are derived from a single abstract Entity named SurveyItem. We are trying to find SurveyItems that contain some query string. I apologize for the wall of code, but I think this is as small as I can make it while staying in our situation. I suspect the problem is caused by having object inheritance and using an abstract root. When using the method below with Entities without inheritance all is fine.

Our entities are stored using TpH and look like this:

public abstract class SurveyItem
{
    [ScaffoldColumn(false)]
    public int Id { get; set; }

    [ScaffoldColumn(false)]
    public virtual ICollection<Survey_SurveyItem> SurveyLinks { get; private set; }

    [ScaffoldColumn(false)]
    public abstract string Identifier { get; }

    public SurveyItem()
    {
        SurveyLinks = new List<Survey_SurveyItem>();
    }
}

public class SurveyHeaderItem : SurveyItem
{
    [Required]
    [Display(Name = "title", ResourceType = typeof(Caracal.Resources.GUI))]
    public Translated Title { get; set; }

    [ScaffoldColumn(false)]
    public override string Identifier
    {
        get { return Title.NL + " / " + Title.EN; }
    }
    // ...
}

public class SurveyQuestion : SurveyItem
{
    [Required]
    [Order(1)]
    [Display(Name = "question", ResourceType = typeof(Caracal.Resources.GUI))]
    public Translated Question { get; set; }

    [Order(2)]
    [Display(Name = "description", ResourceType = typeof(Caracal.Resources.GUI))]
    public TranslatedMultilineOptional Description { get; set; }
    //...
}

The abstract root class does not contain the fields we want to be able to inspect so something like the following is not possible:

Context.SurveyItems.Where(x => x.Field.contains(q));

So what we tried is the following Stored Procedure combined with the code below:

CREATE PROCEDURE SearchSurveyItems (@Q nvarchar(255))
AS
BEGIN
    SET NOCOUNT ON;
    SELECT i.*
    FROM [dbo].SurveyItems AS i
    WHERE
        i.Question_NL LIKE ('%' + @Q + '%') OR
        i.Question_EN LIKE ('%' + @Q + '%') OR
        i.Description_NL LIKE ('%' + @Q + '%') OR
        i.Description_EN LIKE ('%' + @Q + '%') OR
        i.Title_NL LIKE ('%' + @Q + '%') OR
        i.Title_EN LIKE ('%' + @Q + '%') OR
        i.Text_NL LIKE ('%' + @Q + '%') OR
        i.Text_EN LIKE ('%' + @Q + '%')
END

And the call from C#:

public IEnumerable<SurveyItem> SearchSurveyItems(string q)
{
    return this.Database.SqlQuery<SurveyItem>("dbo.SearchSurveyItems @Q", new SqlParameter("Q", q)).ToList();
}

This results in the error from the title:

System.ArgumentNullException was unhandled by user code
  Message=Value cannot be null.
Parameter name: constructor
  Source=System.Core
  ParamName=constructor
  StackTrace:
       at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments)
       at System.Data.Common.Internal.Materialization.Translator.Emit_ConstructEntity(EntityType oSpaceType, IEnume开发者_如何学JAVArable`1 propertyBindings, Expression entityKeyReader, Expression entitySetReader, TranslatorArg arg, EntityProxyTypeInfo proxyTypeInfo)
       at System.Data.Common.Internal.Materialization.Translator.Visit(EntityColumnMap columnMap, TranslatorArg arg)
       at System.Data.Query.InternalTrees.EntityColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
       at System.Data.Common.Internal.Materialization.Translator.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue)
       at System.Data.Common.Internal.Materialization.Translator.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg)
       at System.Data.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
       at System.Data.Common.Internal.Materialization.Translator.TranslateColumnMap[TRequestedType](QueryCacheManager queryCacheManager, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean valueLayer)
       at System.Data.Objects.ObjectContext.InternalTranslate[TElement](DbDataReader reader, String entitySetName, MergeOption mergeOption, Boolean readerOwned)
       at System.Data.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, MergeOption mergeOption, Object[] parameters)
       at System.Data.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, Object[] parameters)
       at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery[TElement](String sql, Object[] parameters)
       at System.Data.Entity.Internal.InternalContext.ExecuteSqlQueryAsIEnumerable[TElement](String sql, Object[] parameters)
       at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery(Type elementType, String sql, Object[] parameters)
       at System.Data.Entity.Internal.InternalSqlNonSetQuery.GetEnumerator()
       at System.Data.Entity.Internal.InternalSqlQuery`1.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Caracal.Entities.CaracalContext.SearchSurveyItems(String q) in J:\Caracal\Entities\CaracalContextDataAccessors.cs:line 48
       at Caracal.Application.Controllers.SearchController.SurveyItem(String q) in J:\Caracal\application\Controllers\SearchController.cs:line 75
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException:

Update

This problem is fixed when I rename the class SurveyItem to AbstractSurveyItem and I create a new class SurveyItem, inheriting from AbstractSurveyItem. It can then create the SurveyItem objects perfectly.

However, I do not consider this a real solution as you now loose the safety and features of the abstract-mechanism. Is there a way I can keep the class abstract but that EF can still construct it somehow? It has the type of the class in each row, so in theory at least it should have enough information to create the correct instance.

Update 2*

In fact, the solution from the first update does remove the exception, however it gives also the following problem. Namely every object that comes out is only a SurveyItem, and never a subclass, resulting in a useless object.


You could cast your SurveyItems collection to the correct type before trying to query them? Perhaps something like:

string q = "[some search term]";
Context.SurveyItems.Where(x => x is SurveyQuestion).Where(x => x.Field.Contains(q));

I might expand that a bit for readability, like so:

string q = "[some search term]";
IEnumerable<SurveyQuestion> surveyQuestions = Context.SurveyItems.Where(x => x is SurveyQuestion);
IEnumerable<SurveyQuestion> matchingSurveyQuestions = surveyQuestions.Where(x => x.Field.Contains(q));

Or for even better readability (at least in my eyes - I like shorter code):

var searchTerm = "[some search term]";
var questions = Context.SurveyItems.Where(si => si is SurveyQuestion);
var matchingQuestions = questions.Where(q => q.Field.Contains(searchTerm));


You can use Table per hierarchy it use discriminator to separate type then you have single table to query and also in your case it should be a performance hit. However, database table is not normalized and all field should be nullable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜