-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArgumentNullException when FromSql used on a type that is not in the model #4227
Comments
@paul-wade could you share a code listing that we can use to reproduce the issue. Below is the code that I tried to reproduce the issue with (using RC1), and it successfully returned results. using Microsoft.Data.Entity;
using System;
using System.Linq;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new BloggingContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.Database.ExecuteSqlCommand("CREATE PROCEDURE GetBlogs AS BEGIN SELECT * FROM dbo.Blog END");
db.Blogs.Add(new Blog { Url = "sample.com" });
db.SaveChanges();
}
using (var db = new BloggingContext())
{
var query = db.Blogs.FromSql("[dbo].[GetBlogs]");
foreach (var item in query)
{
Console.WriteLine(item.Url);
}
}
}
}
class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Sample;Trusted_Connection=True;");
}
}
class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
} |
I found the problem. There was no reference to the model "StudentStateFromProc" in the dbcontext. So we added public DbSet sproctest{ get; set; } Then the set call started working. |
@paul-wade thanks, yep I can reproduce that. It's negative case (at least for the moment), but leaving this open as we should fail more gracefully. |
Ive looked at the unit tests around calling a stored procedure and I am getting the following error.
Value cannot be null.\r\nParameter name: entityType
Below is the code making the call. StudentStateFromProc is a basic Poco with two props matching the names of the select in the sproc. The stored procedure has no parameters.
var asSproc = _context.Set<StudentStateFromProc>().FromSql("[dbo].[prc_MSStudentState_GetSessions]").ToList();
STACK
at Microsoft.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
at Microsoft.Data.Entity.SqlServerMetadataExtensions.SqlServer(IEntityType entityType)
at Microsoft.Data.Entity.Metadata.SqlServerAnnotationProvider.For(IEntityType entityType)
at Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)
at Microsoft.Data.Entity.Query.ExpressionVisitors.EntityQueryableExpressionVisitor.VisitConstant(ConstantExpression constantExpression)
at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)
The text was updated successfully, but these errors were encountered: