EF 5.x DbContext Fluent Generator for C#


A project item to generate Entities, DbContext and Fluent mapping classes. Use this template when working with an EF 5.x release.

(3) Review
Visual Studio
Download (18,646)
E-mail Twitter del.icio.us Digg Facebook
Add to favorites
Reviews (3)
Q and A (25)
Sign in to write a review
Sort by:

by Scott_Scott_Scott | January 09 2014

Just what I needed. Allowed me to use DB first and get EF 5 working with an existing stored procedure call accessed via Imported Function. Out of the box this would result in a type mismatch error when returning from the ExectueFunction. With this and some minor changes in the template file was able to get running properly.

by Levi E | November 01 2013

I was very hopeful that this would work since it would fulfill a real need, but it doesn't support complex types and throws errors when I try to run it.

by Hasibul Haque | May 14 2013

Sign in to start a discussion

  • Utility of the fluent mapping code?!
    4 Posts | Last post August 29, 2013
    • This might sound like a silly question, but I'm really puzzled, so please don't get annoyed:
      What's the added utility of this template over the Reverse Engineer Code First command?
      I mean, given that the generated fluent mapping classes cannot be edited or modified, we can't really upgrade our model as a CodeFirst approach would suggest, use Code Migrations, etc...
      I'm seeing that both this template and the Reverse Engineer Code First command require a connection to a database so as to be able to infer the mappings. (Correct?)
      The DbContext and fluent mapping classes generated by the Reverse Engineer Code First command allow for acquiring total control of the code, because they're a one-time generation of code.
      Please shade some light as to how to exploit your template. I'm really keen to use something out that would allow me for a sweet round trip between CodeFirst and using the Entity Model Designer...
      Thanks in advance...
    • This template useful for projects with pre-existing databases.
    • Fernando,
      The different is, this generates for your existing EDMX model. So, i you have changed the entities, set up inheritance, etc then you reall need this. If your EDMX is nothing more than a 1-1 mapping with no name/navigation changes etc then you can just generate your model/mappings right from the database.
      Although, I'm not sure if that tool generates fluent mappings where your db doesn't use code first conventions.
    • Of course, once you do the generation you would throw away the EDMX and maintain the mappings/entities using code from then on out, hence moving over to code first 100%.
  • Automatic Code Generation
    4 Posts | Last post May 06, 2013
    • Hi... I created a new edmx, and imediatly deleted all other files it automatically created (just kept the edmx).
      Once I  finished modeling, I added a new code generation Item, and selected EF 5.x DbContext Fluent Generator for C#.
      It created three files:
      The problem now is that when I update my model from the designer, lets say change a data type from int to String, that change is not automatically syncronized with the model classes. So  each time I want to update my model, I have to delete every class generated and add a code generation item again.
      Is there away to acomplish automatic syncronization?
    • It's very strange. Is default template of Microsoft works correctly? 
    • It's not quite automatic. Have you tried running the "Transform All T4 Templates" function? In VS 2012 it is an item under the "Build" menu.
    • I have the same issue as BeWilled's. I have to manually run EF 5.x DbContext Generator to overwrite the gen files. Can someone share a solution?
  • NullReferenceExcpetion Mapping.tt line 369
    1 Posts | Last post February 14, 2013
    • Apparently the propertyNode didn't have a value attribute. I changed the code to below and the error no longer occurs. Is this not expected? Is there an issue with my edmx that needs to be corrected?
      string val= propertyNode.Attributes["Value"] != null ? propertyNode.Attributes["Value"].Value : string.Empty ;
  • Enum bug-fix
    1 Posts | Last post January 29, 2013
    • To fix the Enum bug in mapping.tt replace the lines 95 and 272 with:
      PrimitiveType edmType = property.TypeUsage.EdmType as PrimitiveType;
  • Cant find EF 5.x DbContext Fluent Generator for C#
    1 Posts | Last post January 21, 2013
    • Searched NuGet, web site and through VS29012, does not find EF 5.x DbContext Fluent Generator for C#
      I see it here as a download but it makes me wonder what is up with it, typically that means it is replaced by sopmething else or depricated.
      Which isd it? 
  • IsForeignKey fix
    1 Posts | Last post January 15, 2013
    • Hi I.Fle
        I use a one2many with an uncertain FK. Can you fixed it? Thank you
      line 130 	var	 navigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity&&
      		        np.ToEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many );
      line 150  if (associationType.IsForeignKey )
                      if (associationType.ReferentialConstraints.Single().ToProperties.Count == 1)
                          navPropBuilder.AppendFormat(".HasForeignKey(d => d.{0})",
                          navPropBuilder.AppendFormat(".HasForeignKey(d => new {{{0}}})",
                                                      string.Join(", ",
                                                                                 .ToProperties.Select(p => "d." + p.Name)));
  • Could not Find FunctionImport in Container DBEntities
    3 Posts | Last post January 15, 2013
    • Hi, 
      I am not able to call stored procedure using Function import getting an error "Function Import <FunctionName> could not be found in teh container DbEntities". I don't see the template generated any mapping between FunctionImport and teh actual stored procedure in the database. 
      Please help. 
      In Context.cs
      public virtual ObjectResult<PolicyResult> PolicyByRfb(Nullable<int> rfb_id)
                  var rfb_idParameter = rfb_id.HasValue ?
                      new ObjectParameter("rfb_id", rfb_id) :
                      new ObjectParameter("rfb_id", typeof(int));
                  return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PolicyResult>("PolicyByRfb", rfb_idParameter);
    • Hi I.Fle
      Can you please answer my question? 
    • Hi,
      This template does not support SP calling. Sorry.
  • .Mapping.tt error - no navigation property back reference
    4 Posts | Last post December 13, 2012
    • Hi,
      sometimes there is no navigation property back reference. in this case, the generation breaks.
      here is the fix:
      line 130:
                NavigationProperty navPropertyBackReference = navProperty.ToEndMember.GetEntityType().NavigationProperties
                                   .Where(npBack => npBack.RelationshipType == navProperty.RelationshipType && npBack != navProperty)
      		 if (navPropertyBackReference == null) continue;
      the SingleOrDefault call replaces the Single call.
      the if-check after that is important.
      best regards and thank you for this thing!
    • Thanks. I will add your fix.
    • Thanks so much Bernhard, this issue has been plaguing me. I hope an update with this fix is due out soon. 
    • Done. Thanks
  • More issues with enums
    2 Posts | Last post October 10, 2012
    • Like described in posts here, I receive this error: 
      Running transformation: System.InvalidCastException: Unable to cast object of type 'System.Data.Metadata.Edm.EnumType' to type 'System.Data.Metadata.Edm.PrimitiveType'
      I copied over the suggested fix posted here but now I'm receiving a NullReferenceException when trying to compile. I've never used templates before so I'm not sure how I can provide you with further information, please advise. 
    • I cannot reproduce this issue. Please explain what you did
  • Dictionary argumentexception...
    3 Posts | Last post September 24, 2012
    • Getting the following exception when I add the templates to my EDMX file:
      Error	1	Running transformation: System.ArgumentException: An item with the same key has already been added.
         at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
         at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
         at Microsoft.VisualStudio.TextTemplating9EB94C46CE01D11B6064F705B534843E3CBD4FC4D10C69E2CE259E27576C472306C98B02FD5DFF3AE8CC4C6F930F5FC10F7CA67D7A6B3C4F093CF94351AF14F6.GeneratedTextTransformation.BuildEntityMappings(XmlNode mappingNode, XmlNamespaceManager nsmgr, IEnumerable`1 entityTypes, IEnumerable`1 entitySets, IEnumerable`1 tableSets)
         at Microsoft.VisualStudio.TextTemplating9EB94C46CE01D11B6064F705B534843E3CBD4FC4D10C69E2CE259E27576C472306C98B02FD5DFF3AE8CC4C6F930F5FC10F7CA67D7A6B3C4F093CF94351AF14F6.GeneratedTextTransformation.LoadMetadata(String inputFile)
         at Microsoft.VisualStudio.TextTemplating9EB94C46CE01D11B6064F705B534843E3CBD4FC4D10C69E2CE259E27576C472306C98B02FD5DFF3AE8CC4C6F930F5FC10F7CA67D7A6B3C4F093CF94351AF14F6.GeneratedTextTransformation.TransformText()	C:\temp2\Domain.CdrBC\Model1.Mapping.tt	1	1
    • I've started to figure out what's going on.  I've got an EntityTypeMapping node that has 2 MappingFragment nodes.  This is causing the T4 to throw at line 434 on the dictionary.Add().
    • I cannot reproduce this issue. If you fixed it, please share with your solution. Thanks. 
11 - 20 of 25 Items