EF 5.x DbContext Fluent Generator for C#

Free

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

(4) Review
Visual Studio
2012
Download (20,570)
12/13/2012
0.0.1.0
View
E-mail Twitter del.icio.us Digg Facebook
Add to favorites
Description
Reviews (4)
Q and A (27)
Sign in to write a review
Sort by:

by interwanderer | June 04 2015


Really handy tool and works for me. Steps that I've used can be found here (there are a few gotcha's):

http://www.mashitup.be/from-edmx-database-first-to-code-first/

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


  • Errors using mappings
    1 Posts | Last post September 04, 2013
    • I am getting the following errors when querying against my model:
      
      Invalid column name 'RetainedEarningsAccount_Key'.
      Invalid column name 'PLSummaryAccount_Key'.
      
      Those are not even property names in my model class. It seems like the fluent generator is setting something to override some configuration default, but I have no idea what that could be. 
      
      Does anyone have any ideas?
      
      BOb
      
  • Error when mapping TPH
    2 Posts | Last post Thu 7:40 PM
    • I am getting the following error running the tool on a model that has a TPH inheritence mapped:
      
      Error	8	Running transformation: System.NullReferenceException: Object reference not set to an instance of an object.
         at Microsoft.VisualStudio.TextTemplatingF57CE23A0768E2A7B5E7D95FF29134F0B0197E50FF39F824E5B72D1480D777076FEA484BA645024FF81FFAFBC80B58C979072212B9706D02A2A4D5EC53A89FD2.GeneratedTextTransformation.BuildTPHMappings(XmlNode mappingNode, XmlNamespaceManager nsmgr, IEnumerable`1 entityTypes, IEnumerable`1 entitySets, IEnumerable`1 tableSets)
         at Microsoft.VisualStudio.TextTemplatingF57CE23A0768E2A7B5E7D95FF29134F0B0197E50FF39F824E5B72D1480D777076FEA484BA645024FF81FFAFBC80B58C979072212B9706D02A2A4D5EC53A89FD2.GeneratedTextTransformation.LoadMetadata(String inputFile)
         at Microsoft.VisualStudio.TextTemplatingF57CE23A0768E2A7B5E7D95FF29134F0B0197E50FF39F824E5B72D1480D777076FEA484BA645024FF81FFAFBC80B58C979072212B9706D02A2A4D5EC53A89FD2.GeneratedTextTransformation.TransformText()	
      
      I can provide the edmx if that will help.
      
      
      
    • ok, I think I see what's going on here. My EDMX has a TPH relationship where the subclass is discriminated with a Is Not Null and the parent class with Is Null. My code change below got rid of the error, but the mapping isn't correct. How would you map HasValue(not null) ??? Is that even possible? 
  • Utility of the fluent mapping code?!
    4 Posts | Last post Thu 5:56 PM
    • 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:
      
      entities.Context
      entities.Entities
      entities.Mappings
      
      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? 
      
      thanks
      
      JAMES
  • 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})",
                                                      associationType.ReferentialConstraints.Single()
                                                                     .ToProperties.Single()
                                                                     .Name);
                      else
                          navPropBuilder.AppendFormat(".HasForeignKey(d => new {{{0}}})",
                                                      string.Join(", ",
                                                                  associationType.ReferentialConstraints.Single()
                                                                                 .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)
                                   .SingleOrDefault();
      		 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
11 - 20 of 27 Items