c# - Entity Framework Column Mapping -
i have issue mapping object property column database function.
the database function returns column called [on hand]
. therefore model property called onhand
.
this not map correctly , fails retrieve data correctly column.
i have attempted following in order resolve this:
editing model use annotation
[column("on hand")] public int onhand { get; set; }
using fluent api
modelbuilder.entity<bindetail>() .property(e => e.onhand) .hascolumnname("on hand");
neither of these approaches have worked either or independently.
the way can work on test database alter return column of function [onhand]
, however, due other systems using function, not option use on live database.
any suggestions has appreciated
if you're using entity framework core 1.0 rc 1, there bug (it's fixed @ rc2 , onwards) causes this.
a workaround ordering fields z, quick sample:
"select " + getcolumnnames<unit>("r") + " unit r"
helper methods:
private static dictionary<type, propertyinfo[]> getpropertiescache = new dictionary<type, propertyinfo[]>(); public static string getcolumnnames<t>(string prefix) { var columns = getproperties(typeof(t)).orderby(i => i.name).select(i => $"[{prefix}].[{i.name}]"); return string.join(", ", columns); } public static ienumerable<propertyinfo> getproperties(type type) { if (getpropertiescache.containskey(type)) return getpropertiescache[type].asenumerable(); var properties = type .gettypeinfo() .declaredproperties; getpropertiescache.add(type, properties.toarray()); return getpropertiescache[type].asenumerable(); }
Comments
Post a Comment