Reverse Engineer Code first on VS2013 using EF Power Tools is generated backwards.
The decimal (18, 4) field in the discovery database has no precision and scale in the generated mapping class.
This makes the automatically generated SQL default use of decimal (18, 2) when data is saved through EF.
Fortunately EF Power Tools provides the Customize Reverse Engineer Templates and gives the TT files it uses.
Open its mapping.tt.
See
if(Type. Clrequivalenttype = =typeof(int) || Type. Clrequivalenttype = =typeof(decimal) || Type. Clrequivalenttype = =typeof( Short) || Type. Clrequivalenttype = =typeof(Long)) { if(IsKey && StoreGeneratedPattern! =storegeneratedpattern.identity) {Configlines.add (". Hasdatabasegeneratedoption (Databasegeneratedoption.none)"); } Else if((!iskey | | efHost.EntityType.KeyMembers.Count >1) && StoreGeneratedPattern = =storegeneratedpattern.identity) {Configlines.add (". Hasdatabasegeneratedoption (databasegeneratedoption.identity)"); } }
The decimal did not deal with precision.
Add the following code:
if(Type. Clrequivalenttype = =typeof(decimal)) { //foreach (var f in Prop. Typeusage.facets)//{ //var scale = (Facet) F; //WriteLine ("//Name: "+ scale. Name); //} varScale = (FACET) Prop. TypeUsage.Facets.SingleOrDefault (f = f.name = =" Scale"); varPrecision = (Facet) Prop. TypeUsage.Facets.SingleOrDefault (f = f.name = ="Precision"); Configlines.add (string. Format (". Hasprecision ({0},{1})", precision. Value, scale. Value)); }
Use reverse Engineer Code first again.
Get the mapping with precision.
this. Property (t = t.d0) . Hasprecision (0); this. Property (t = t.d2) . Hasprecision (2); this. Property (t = t.d4) . Hasprecision (4);
T4 template Modifications when the EF Power Tools database is reverse-generated