0

I'm trying to access/use raw Sql script or use stored procedure in C# .Net Core Razor page. I know my sql script/stored procedure works upon getting the script on debug mode and then trying it out in MS SQL Server Management Studio.

I always get NullReferenceException: Object reference not set to an instance of an object on this code:

var bom = _context.Recipe
                .FromSqlRaw("EXEC sproc_GetBOM @itemcode", _itemCode)
                .AsNoTracking()
               .ToList();

This is my context

public partial class MyDBContext: IdentityDbContext
    {
        public MyDBContext()
        {
        }

        public MyDBContext(DbContextOptions<MyDBContext> options)
            : base(options)
        {
        }

        public DbSet<Recipe> Recipe { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Server=serverIp; Database=DB; Integrated Security = False; Trusted_Connection=False; MultipleActiveResultSets=True; User Id=UserId;Password=Password;");
            }
        }
}

protected override void OnModelCreating(ModelBuilder modelBuilder){
//
}

Any idea what am I doing wrong? Thank you in advance.

Seven
  • 11
  • 1
  • https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – nanu_nana Jul 02 '20 at 07:59
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – vasily.sib Jul 02 '20 at 08:01
  • where is ```_context``` and ```_itemCode``` defined. Do you ever use ```OnConfiguring()``` ? – nanu_nana Jul 02 '20 at 08:02
  • _itemcode is just a variable where I pass the parameter. I have my OnConfiguring() above, is it wrong? – Seven Jul 02 '20 at 08:19
  • I will check those links..thank you. – Seven Jul 02 '20 at 08:22

0 Answers0