Incorrect data reader EF 6 with data annotations MVC

Viewed 37

the problem im having is related to the datareader being incompatible to the model I specified, i thought that by adding the Column name it would work, but it doesnt. Here is the code related to the problem at hand.

Model:

[Table("IPs")]
    public class Ip : BaseEntity
    {
        [Key]
        public int Id { get; set; }

        [Column("IdItem")]
        public int? ItemId { get; set; }

        [Column("EnUso")]
        public string En_Uso { get; set; }

        //more properties...
    }

The method that is failing works with generics, the ModelClass would be Ip here:

        var table = TableName<ModelClass>();
        var primaryKey = PrimaryKeyProperty<ModelClass>().Name; //this one uses reflection to see which one get the Key
        var query = String.Format(FindQuery(), table, primaryKey, id); 
        // The resulting query is this for example: Select * from IPs WHERE id = 1 
        var result = DBSet().SqlQuery(query); //here it fails
        return result.FirstOrDefault();

The error im getting:

the data reader is incompatible with the specified 'Inventario.Models.Ip'. A member of the type, 'ItemId', does not have a corresponding column in the data reader with the same name.

If i change the column name to IdItem, then it breaks on "En_Uso", while changing some of the properties wouldnt be a problem I would like to fix it if possible in a way with data annotations or something of the sort that doesnt requiere to use the exact table names, but I already specified the column name i dont know what else. to do

0 Answers
Related