Skip to content
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

Closed
paul-wade opened this issue Jan 5, 2016 · 4 comments
Closed

Comments

@paul-wade
Copy link

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)

@rowanmiller
Copy link
Contributor

@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; }
    }
}

@paul-wade
Copy link
Author

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.

@rowanmiller rowanmiller changed the title Executing Stored Procedure returns error Value cannot be null.\r\nParameter name: entityType ArgumentNullException when FromSql used on a type that is not in the model Jan 6, 2016
@rowanmiller
Copy link
Contributor

@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.

@rowanmiller
Copy link
Contributor

The ability to use raw SQL to query for types that are not in the model is tracked by #1862
Opened #4301 to track blocking folks creating sets for types that are not in the model

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants