Problem materializing a view
Describe what is not working as expected.
I am getting an error when I try to materialize a view from SQL Server. I'm hoping someone can point out what I might be doing wrong.
Error: Expression of type 'System.Object' cannot be used for parameter of type 'Microsoft.EntityFrameworkCore.Metadata.IKey' of method 'System.Collections.Generic.IEnumerable`1[WinsApp.Core.DbViews.SponsorMostRecentAddressView]
I have a very simple view that retrieves addresses. The SQL is quite complex but the view returns just a few fields. It is declared like this:
public virtual DbQuery<SponsorMostRecentAddressView> SponsorMostRecentAddresses { get; set; }
In the OnModelCreating method, I have this code:
modelBuilder.Query<SponsorMostRecentAddressView>()
.ToView("dbo.vwMostRecentAddress");
The class that holds the view results is very simple:
public class SponsorMostRecentAddressView
{
[Key]
public int AddressId { get; }
public int SponsorId { get; }
public string SponsorName { get; }
public int? SiteId { get; }
public string SiteName { get; }
public int LookupAddressTypeId { get; }
public string LookupAddressType { get; }
public string PrimaryAddress { get;}
public string SecondaryAddress { get; }
public string City { get; }
public string County { get; }
public string State { get; }
public string ZipCode { get; }
public string ZipCodePlus4 { get; }
public int RowRank { get; }
}
When I try to materialize the results using .ToList() I get an error.
This does not throw an error:
var addresses = _dbContext.SponsorMostRecentAddresses
.Where(x => x.SponsorId == sponsorId);
But this does:
var addresses = _dbContext.SponsorMostRecentAddresses
.Where(x => x.SponsorId == sponsorId)
.ToList();
If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message:
System.ArgumentException: 'Expression of type 'System.Object' cannot be used for parameter of type 'Microsoft.EntityFrameworkCore.Metadata.IKey' of method 'System.Collections.Generic.IEnumerable`1[WinsApp.Core.DbViews.SponsorMostRecentAddressView]
Stack trace:
at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0, Expression arg1, Expression arg2, Expression arg3, Expression arg4)
at Microsoft.EntityFrameworkCore.InMemory.Query.ExpressionVisitors.Internal.InMemoryEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at WinsApp.Domain.Sponsor.SponsorService.GetSponsorAddresses(Int32 sponsorId) in C:\source\Workspaces\WINS\Dev\WinsApp.Domain.Sponsor\SponsorService.cs:line 199
at WinsApp.Infrastucture.Tests.Sponsor.SponsorServiceTests.SponsorService_GetSponsorAddresses_ReturnsAddresses() in C:\source\Workspaces\WINS\Dev\WinsApp.Infrastucture.Tests\Sponsor\SponsorServiceTests.cs:line 37
Further technical details
EF Core version: 2.1.4.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2017 15.9.0 Preview 3.0
DOH. Never mind. User error. I finally realized I was using the In-Memory provider by mistake. I really thought I checked that before. Sorry to waste anyone's time, if I did.