Mapping a class to a stored procedure resultset "Value cannot be null.Parameter name: entityType"

entity-framework-core

Question

I am using Entity Framework 7 RC1 Code First.

I have the following code

var resultset = await _db.Employees.FromSql<Employee>("dbo.sp_GetEmployees @DeptId="+ deptId).ToListAsync();

this works and fetches the data correctly.

however I need to change the stored proc to contain other properties than employees are returning.

So I adjust the stored proc and create a custom class EmployeeQuery that inherits from Employee with the additional properties.

However FromSql is a method on the DbSet of my DBContext

 public DbSet<Employee> Employees{ get; set; }

so I can do the same for EmployeeQuery by creating

 public DbSet<EmployeeQuery> EmployeeQueryItems{ get; set; }

and var resultset = await _db.EmployeeQueryItems.FromSql("dbo.sp_GetEmployees @DeptId="+ deptId).ToListAsync();

however creating a DBSet property this tells Entity Framework to create the table in the database and it's simply just a query object. So I don't want to do the above.

However apparently you can use the DbContext Set Method as mentioned in github and in this answer

so I change my code to var resultset= await _db.Set().FromSql("dbo.sp_GetEmployees @DeptId="+ deptId).ToListAsync();

However this gives me a runtime error

ex = {"Value cannot be null.\r\nParameter name: entityType"}

How do I get around this?

From this gitbub issue it doesn't seem possible without creating a dummy table.

Is there not a way to tell EF to not create the table? Like a NotMapped attribute on the DbSet?

1
3
5/23/2017 11:52:30 AM

Popular Answer

I ran across this issue myself, and the only way I was able to resolve the problem was mapping a key for the POCO.

In a partial class along side the generated context, I override OnModelCreating and add:

modelBuilder.Entity<ClassName>(entity => entity.HasKey(e => e.Id));

Seems to satisfy EntityFramework.

3
6/21/2016 5:08:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow