3

I'm using Entity Framework. I am calling a stored procedure that returns multiple data sets. One of the result sets maps to an entity that I have defined an enumeration for, and have defined a string backing property to receive the result and parse out the enumeration.

public class OrderInfo
{
    [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid orderID { get; set; }
    [NotMapped]
    public BillingStatus billingStatus { get; set; }
    [Required, Column("billingStatus")]
    public string billingStatusString
    {
        get { return billingStatus.ToString(); }
        private set { billingStatus = (BillingStatus)Enum.Parse(typeof(BillingStatus), value); }
    }
}

Unfortunately, the annotations are ignored when calling db.ObjectContext.Translate( ... ). I get an error message

var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "[dbo].[GetOrders]";
cmd.CommandType = System.Data.CommandType.StoredProcedure;

db.Database.Connection.Open();
var reader = cmd.ExecuteReader();

var orders = ((IObjectContextAdapter)db)
               .ObjectContext
               .Translate<OrderInfo>(reader, "Orders", MergeOption.AppendOnly)
               .ToList();
reader.NextResult();

The mappings defined in the data annotations are not respected, and the Translate method tries to bind to the backing property by its actual name billingStatusString rather than its mapped value, billingStatus.

A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.dll

Additional information: The data reader is incompatible with the specified 'Orders.OrderInfo'. A member of the type, 'billingStatusString', does not have a corresponding column in the data reader with the same name.

Anyone know why this is, or have a workaround?

DavidG
  • 113,891
  • 12
  • 217
  • 223
kindasimple
  • 2,427
  • 1
  • 16
  • 20
  • 1
    You may have to map the database column to a string property called `billingStatus` and then have another property to retrieve the enum (i.e. `billingStatusEnum`) – DavidG Aug 23 '16 at 00:40
  • It clearly says the error no `'billingStatusString', does not have a corresponding column in the data reader with the same name` ? – Sampath Aug 23 '16 at 01:16
  • @Sampath, and I expect the attribute `Column("billingStatus"`) that is applied to the `billingStatusString` property should inform EF that the data mapping is from billingStatus => billingStatusString. It obviously doesn't when calling Translate, but I'm looking for some clarity. Why this is a bad idea? – kindasimple Aug 23 '16 at 18:41
  • Did you find a solution to this as I have a very similar issue? – Ben Sep 22 '17 at 10:51

0 Answers0