I have a legacy mvc application which I cannot rewrite. There's a stored procedure which all the returns column names of table A except for ID. (Note, it actually does not select from table A, but just happens to return the same column names). I wish to call the stored procedure (via SQL query), and assign the results to a variable of class A. However, I get a System.Data.Entity.Core.CommandExecutionException saying A member of the type 'ID' does not have a corresponding column in the data reader with the same name.
My code is as follows:
Class A
namespace LegacyApp.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
public partial class A
{
[NotMapped]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public Nullable<int> SOId { get; set; }
public string SODesc { get; set; }
//...
}
}
My Controller
public class MyController : Controller
{
private MyDBContext db = new MyDBContext();
//...
[HttpPost]
[ValidateInput(false)]
[OutputCache(NoStore = true, Duration = 0)]
public ActionResult Index(ViewModel rb)
{
var query = this.buildQuery().ToString();
// The results returned by the query contain all members of A except for ID
IEnumerable<A> goals = db.Database.SqlQuery<A>(query);
// Exception occurs here
List<A> goalList = goals.ToList();
rb.goalList;
return View(rb);
}
}
Properties of ID in .edmx designer
I was under the assumption that the [NotMapped] or [DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation should ignore the ID? The reason I'm doing this is because the user would be filtering the queried dataset, in which the filtered results would then be inserted into table A. As table A using has a PK ID which is auto incremented, I would like the IDs to be NULL upon insertion. I've updated my Model from the entity designer but I still get the error. What is the proper way to do this?
