Statistics
31
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-01-05

AuthorGoalKicker.com

No description

Tags
No tags
Publisher: GoalKicker.com
Publish Year: 2018
Language: 英文
Pages: 94
File Format: PDF
File Size: 1.5 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Entity Framework Notes for ProfessionalsEntity Framework Notes for Professionals GoalKicker.com Free Programming Books Disclaimer This is an unocial free book created for educational purposes and is not aliated with ocial Entity Framework group(s) or company(s). All trademarks and registered trademarks are the property of their respective owners 80+ pages of professional hints and tricks
Contents About 1 ................................................................................................................................................................................... Chapter 1: Getting started with Entity Framework 2 ..................................................................................... Section 1.1: Installing the Entity Framework NuGet Package 2 .................................................................................... Section 1.2: Using Entity Framework from C# (Code First) 4 ....................................................................................... Section 1.3: What is Entity Framework? 5 ....................................................................................................................... Chapter 2: Code First Conventions 6 ...................................................................................................................... Section 2.1: Removing Conventions 6 ............................................................................................................................. Section 2.2: Primary Key Convention 6 .......................................................................................................................... Section 2.3: Type Discovery 6 ......................................................................................................................................... Section 2.4: DecimalPropertyConvention 7 ................................................................................................................... Section 2.5: Relationship Convention 9 .......................................................................................................................... Section 2.6: Foreign Key Convention 10 ........................................................................................................................ Chapter 3: Optimization Techniques in EF 11 ..................................................................................................... Section 3.1: Using AsNoTracking 11 ................................................................................................................................ Section 3.2: Execute queries in the database when possible, not in memory 11 ...................................................... Section 3.3: Loading Only Required Data 11 ................................................................................................................. Section 3.4: Execute multiple queries async and in parallel 12 ................................................................................... Section 3.5: Working with stub entities 13 ...................................................................................................................... Section 3.6: Disable change tracking and proxy generation 14 .................................................................................. Chapter 4: .t4 templates in entity framework 15 ........................................................................................... Section 4.1: Dynamically adding Interfaces to model 15 ............................................................................................. Section 4.2: Adding XML Documentation to Entity Classes 15 .................................................................................... Chapter 5: Code First DataAnnotations 17 .......................................................................................................... Section 5.1: [Column] attribute 17 ................................................................................................................................... Section 5.2: [DatabaseGenerated] attribute 17 ............................................................................................................ Section 5.3: [Required] attribute 18 ................................................................................................................................ Section 5.4: [MaxLength] and [MinLength] attributes 18 ............................................................................................ Section 5.5: [InverseProperty(string)] attribute 19 ....................................................................................................... Section 5.6: [ComplexType] attribute 20 ....................................................................................................................... Section 5.7: [ForeignKey(string)] attribute 21 ............................................................................................................... Section 5.8: [Range(min,max)] attribute 21 .................................................................................................................. Section 5.9: [NotMapped] attribute 22 ........................................................................................................................... Section 5.10: [Table] attribute 23 .................................................................................................................................... Section 5.11: [Index] attribute 23 ..................................................................................................................................... Section 5.12: [Key] attribute 24 ....................................................................................................................................... Section 5.13: [StringLength(int)] attribute 25 ................................................................................................................. Section 5.14: [Timestamp] attribute 25 .......................................................................................................................... Section 5.15: [ConcurrencyCheck] Attribute 26 ............................................................................................................. Chapter 6: Database first model generation 27 ............................................................................................... Section 6.1: Generating model from database 27 ......................................................................................................... Section 6.2: Adding data annotations to the generated model 28 ............................................................................. Chapter 7: Model Restraints 30 ................................................................................................................................. Section 7.1: One-to-many relationships 30 .................................................................................................................... Chapter 8: Code First - Fluent API 32 ...................................................................................................................... Section 8.1: Mapping models 32 ...................................................................................................................................... Section 8.2: Composite Primary Key 33 .........................................................................................................................
Section 8.3: Maximum Length 34 .................................................................................................................................... Section 8.4: Primary Key 34 ............................................................................................................................................ Section 8.5: Required properties (NOT NULL) 35 ......................................................................................................... Section 8.6: Explict Foreign Key naming 35 ................................................................................................................... Chapter 9: Loading related entities 37 .................................................................................................................. Section 9.1: Eager loading 37 .......................................................................................................................................... Section 9.2: Explicit loading 37 ........................................................................................................................................ Section 9.3: Lazy loading 38 ............................................................................................................................................ Section 9.4: Projection Queries 38 .................................................................................................................................. Chapter 10: Transactions 40 ........................................................................................................................................ Section 10.1: Database.BeginTransaction() 40 .............................................................................................................. Chapter 11: Managing entity state 41 ..................................................................................................................... Section 11.1: Setting state Added of a single entity 41 ................................................................................................... Section 11.2: Setting state Added of an object graph 41 .............................................................................................. Chapter 12: Entity Framework Code First 43 ...................................................................................................... Section 12.1: Connect to an existing database 43 .......................................................................................................... Chapter 13: Database Initialisers 45 ........................................................................................................................ Section 13.1: CreateDatabaseIfNotExists 45 ................................................................................................................... Section 13.2: DropCreateDatabaseIfModelChanges 45 ................................................................................................ Section 13.3: DropCreateDatabaseAlways 45 ............................................................................................................... Section 13.4: Custom database initializer 45 .................................................................................................................. Section 13.5: MigrateDatabaseToLatestVersion 46 ...................................................................................................... Chapter 14: Complex Types 47 ................................................................................................................................... Section 14.1: Code First Complex Types 47 .................................................................................................................... Chapter 15: Tracking vs. No-Tracking 48 .............................................................................................................. Section 15.1: No-tracking queries 48 ............................................................................................................................... Section 15.2: Tracking queries 48 .................................................................................................................................... Section 15.3: Tracking and projections 48 ...................................................................................................................... Chapter 16: Entity framework Code First Migrations 50 ............................................................................... Section 16.1: Enable Migrations 50 ................................................................................................................................... Section 16.2: Add your first migration 50 ....................................................................................................................... Section 16.3: Doing "Update-Database" within your code 52 ...................................................................................... Section 16.4: Seeding Data during migrations 52 .......................................................................................................... Section 16.5: Initial Entity Framework Code First Migration Step by Step 53 ............................................................. Section 16.6: Using Sql() during migrations 54 .............................................................................................................. Chapter 17: Inheritance with EntityFramework (Code First) 56 ................................................................ Section 17.1: Table per hierarchy 56 ................................................................................................................................ Section 17.2: Table per type 56 ....................................................................................................................................... Chapter 18: Best Practices For Entity Framework (Simple & Professional) 58 ................................. Section 18.1: 1- Entity Framework @ Data layer (Basics) 58 ........................................................................................ Section 18.2: 2- Entity Framework @ Business layer 61 ............................................................................................... Section 18.3: 3- Using Business layer @ Presentation layer (MVC) 64 ....................................................................... Section 18.4: 4- Entity Framework @ Unit Test Layer 66 ............................................................................................. Chapter 19: Entity Framework with PostgreSQL 70 ........................................................................................ Section 19.1: Pre-Steps needed in order to use Entity Framework 6.1.3 with PostgresSql using Npgsqlddexprovider 70 ........................................................................................................................................... Chapter 20: Entity Framework with SQLite 71 ................................................................................................... Section 20.1: Setting up a project to use Entity Framework with an SQLite provider 71 ..........................................
Chapter 21: Advanced mapping scenarios: entity splitting, table splitting 74 .................................. Section 21.1: Entity splitting 74 ......................................................................................................................................... Section 21.2: Table splitting 75 ........................................................................................................................................ Chapter 22: Mapping relationship with Entity Framework Code First: One-to-one and variations 76 ........................................................................................................................................................................ Section 22.1: Mapping one-to-zero or one 76 ................................................................................................................ Section 22.2: Mapping one-to-one 79 ............................................................................................................................ Section 22.3: Mapping one or zero-to-one or zero 80 .................................................................................................. Chapter 23: Mapping relationship with Entity Framework Code First: One-to-many and Many-to-many 81 ............................................................................................................................................................. Section 23.1: Mapping one-to-many 81 .......................................................................................................................... Section 23.2: Mapping one-to-many: against the convention 82 ............................................................................... Section 23.3: Mapping zero or one-to-many 83 ............................................................................................................ Section 23.4: Many-to-many 84 ...................................................................................................................................... Section 23.5: Many-to-many: customizing the join table 85 ........................................................................................ Section 23.6: Many-to-many: custom join entity 86 ..................................................................................................... Credits 89 .............................................................................................................................................................................. You may also like 90 ........................................................................................................................................................
GoalKicker.com – Entity Framework Notes for Professionals 1 About Please feel free to share this PDF with anyone for free, latest version of this book can be downloaded from: https://goalkicker.com/EntityFrameworkBook This Entity Framework Notes for Professionals book is compiled from Stack Overflow Documentation, the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified This is an unofficial free book created for educational purposes and is not affiliated with official Entity Framework group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk Please send feedback and corrections to web@petercv.com
GoalKicker.com – Entity Framework Notes for Professionals 2 Chapter 1: Getting started with Entity Framework Version Release Date 1.0 2008-08-11 4.0 2010-04-12 4.1 2011-04-12 4.1 Update 1 2011-07-25 4.3.1 2012-02-29 5.0 2012-08-11 6.0 2013-10-17 6.1 2014-03-17 Core 1.0 2016-06-27 Release Notes: https://msdn.microsoft.com/en-ca/data/jj574253.aspx Section 1.1: Installing the Entity Framework NuGet Package In your Visual Studio open the Solution Explorer window then right click on your project then choose Manage NuGet Packages from the menu: In the window that opens type EntityFramework in the search box in the top right.
GoalKicker.com – Entity Framework Notes for Professionals 3 Or if you are using Visual Studio 2015 you'll see something like this: Then click Install. We can also install entity framework using the package manager console. To do you have first to open it using the Tools menu -> NuGet Package Manager -> Package Manager Console then enter this:
GoalKicker.com – Entity Framework Notes for Professionals 4 Install-Package EntityFramework This will install Entity Framework and automatically add a reference to the assembly in your project. Section 1.2: Using Entity Framework from C# (Code First) Code first allows you to create your entities (classes) without using a GUI designer or a .edmx file. It is named Code first, because you can create your models first and Entity framework will create database according to mappings for you automatically. Or you can also use this approach with existing database, which is called code first with existing database For example, if you want a table to hold a list of planets: public class Planet { public string Name { get; set; } public decimal AverageDistanceFromSun { get; set; } } Now create your context which is the bridge between your entity classes and the database. Give it one or more DbSet<> properties: using System.Data.Entity; public class PlanetContext : DbContext { public DbSet<Planet> Planets { get; set; } } We can use this by doing the following: using(var context = new PlanetContext()) { var jupiter = new Planet {
GoalKicker.com – Entity Framework Notes for Professionals 5 Name = "Jupiter", AverageDistanceFromSun = 778.5 }; context.Planets.Add(jupiter); context.SaveChanges(); } In this example we create a new Planet with the Name property with the value of "Jupiter" and the AverageDistanceFromSun property with the value of 778.5 We can then add this Planet to the context by using the DbSet's Add() method and commit our changes to the database by using the SaveChanges() method. Or we can retrieve rows from the database: using(var context = new PlanetContext()) { var jupiter = context.Planets.Single(p => p.Name == "Jupiter"); Console.WriteLine($"Jupiter is {jupiter.AverageDistanceFromSun} million km from the sun."); } Section 1.3: What is Entity Framework? Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an O/RM framework called "Entity Framework" to automate database related activities for your application. Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database. What is O/RM? ORM is a tool for storing data from domain objects to the relational database like MS SQL Server, in an automated way, without much programming. O/RM includes three main parts: Domain class objects1. Relational database objects2. Mapping information on how domain objects map to relational database objects(e.x tables, views & stored3. procedures) ORM allows us to keep our database design separate from our domain class design. This makes the application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update & Delete) so that the developer doesn't need to write it manually.
GoalKicker.com – Entity Framework Notes for Professionals 6 Chapter 2: Code First Conventions Section 2.1: Removing Conventions You can remove any of the conventions defined in the System.Data.Entity.ModelConfiguration.Conventions namespace, by overriding OnModelCreating method. The following example removes PluralizingTableNameConvention. public class EshopContext : DbContext { public DbSet<Product> Products { set; get; } . . . protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } By default EF will create DB table with entity class name suffixed by 's'. In this example, Code First is configured to ignore PluralizingTableName convention so, instead of dbo.Products table dbo.Product table will be created. Section 2.2: Primary Key Convention By default a property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column. Simple Example: public class Room { // Primary key public int RoomId{ get; set; } ... } Section 2.3: Type Discovery By default Code First includes in model Types defined as a DbSet property in context class.1. Reference types included in entity types even if they are defined in different assembly.2. Derived classes even if only the base class is defined as DbSet property3. Here is an example, that we are only adding Company as DbSet<Company> in our context class: public class Company { public int Id { set; get; } public string Name { set; get; } public virtual ICollection<Department> Departments { set; get; } } public class Department { public int Id { set; get; }
GoalKicker.com – Entity Framework Notes for Professionals 7 public string Name { set; get; } public virtual ICollection<Person> Staff { set; get; } } [Table("Staff")] public class Person { public int Id { set; get; } public string Name { set; get; } public decimal Salary { set; get; } } public class ProjectManager : Person { public string ProjectManagerProperty { set; get; } } public class Developer : Person { public string DeveloperProperty { set; get; } } public class Tester : Person { public string TesterProperty { set; get; } } public class ApplicationDbContext : DbContext { public DbSet<Company> Companies { set; get; } } We can see that all the classes are included in model Section 2.4: DecimalPropertyConvention By default Entity Framework maps decimal properties to decimal(18,2) columns in database tables.
GoalKicker.com – Entity Framework Notes for Professionals 8 public class Box { public int Id { set; get; } public decimal Length { set; get; } public decimal Width { set; get; } public decimal Height { set; get; } } We can change the precision of decimal properties: 1.Use Fluent API: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Box>().Property(b => b.Width).HasPrecision(20, 4); } Only "Width" Property is mapped to decimal(20, 4). 2.Replace the convention: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<DecimalPropertyConvention>(); modelBuilder.Conventions.Add(new DecimalPropertyConvention(10, 4)); }
GoalKicker.com – Entity Framework Notes for Professionals 9 Every decimal property is mapped to decimal(10,4) columns. Section 2.5: Relationship Convention Code First infer the relationship between the two entities using navigation property. This navigation property can be a simple reference type or collection type. For example, we defined Standard navigation property in Student class and ICollection navigation property in Standard class. So, Code First automatically created one-to-many relationship between Standards and Students DB table by inserting Standard_StandardId foreign key column in the Students table. public class Student { public int StudentID { get; set; } public string StudentName { get; set; } public DateTime DateOfBirth { get; set; } //Navigation property public Standard Standard { get; set; } } public class Standard { public int StandardId { get; set; } public string StandardName { get; set; } //Collection navigation property public IList<Student> Students { get; set; } } The above entities created the following relationship using Standard_StandardId foreign key.
GoalKicker.com – Entity Framework Notes for Professionals 10 Section 2.6: Foreign Key Convention If class A is in relationship with class B and class B has property with the same name and type as the primary key of A, then EF automatically assumes that property is foreign key. public class Department { public int DepartmentId { set; get; } public string Name { set; get; } public virtual ICollection<Person> Staff { set; get; } } public class Person { public int Id { set; get; } public string Name { set; get; } public decimal Salary { set; get; } public int DepartmentId { set; get; } public virtual Department Department { set; get; } } In this case DepartmentId is foreign key without explicit specification.
GoalKicker.com – Entity Framework Notes for Professionals 11 Chapter 3: Optimization Techniques in EF Section 3.1: Using AsNoTracking Bad Example: var location = dbContext.Location .Where(l => l.Location.ID == location_ID) .SingleOrDefault(); return location; Since the above code is simply returning an entity without modifying or adding it, we can avoid tracking cost. Good Example: var location = dbContext.Location.AsNoTracking() .Where(l => l.Location.ID == location_ID) .SingleOrDefault(); return location; When we use function AsNoTracking() we are explicitly telling Entity Framework that the entities are not tracked by the context. This can be especially useful when retrieving large amounts of data from your data store. If you want to make changes to un-tracked entities however, you must remember to attach them before calling SaveChanges. Section 3.2: Execute queries in the database when possible, not in memory Suppose we want to count how many counties are there in Texas: var counties = dbContext.States.Single(s => s.Code == "tx").Counties.Count(); The query is correct, but inefficient. States.Single(…) loads a state from the database. Next, Counties loads all 254 counties with all of their fields in a second query. .Count() is then performed in memory on the loaded Counties collection. We've loaded a lot of data we don't need, and we can do better: var counties = dbContext.Counties.Count(c => c.State.Code == "tx"); Here we only do one query, which in SQL translates to a count and a join. We return only the count from the database - we've saved returning rows, fields, and creation of objects. It is easy to see where the query is made by looking at the collection type: IQueryable<T> vs. IEnumerable<T>. Section 3.3: Loading Only Required Data One problem often seen in code is loading all the data. This will greatly increase the load on the server. Let's say I have a model called "location" that has 10 fields in it, but not all the fields are required at the same time. Let's say I only want the 'LocationName' parameter of that model. Bad Example
GoalKicker.com – Entity Framework Notes for Professionals 12 var location = dbContext.Location.AsNoTracking() .Where(l => l.Location.ID == location_ID) .SingleOrDefault(); return location.Name; Good Example var location = dbContext.Location .Where(l => l.Location.ID == location_ID) .Select(l => l.LocationName); .SingleOrDefault(); return location; The code in the "good example" will only fetch 'LocationName' and nothing else. Note that since no entity is materialized in this example, AsNoTracking() isn't necessary. There's nothing to be tracked anyway. Fetching more fields with Anonymous Types var location = dbContext.Location .Where(l => l.Location.ID == location_ID) .Select(l => new { Name = l.LocationName, Area = l.LocationArea }) .SingleOrDefault(); return location.Name + " has an area of " + location.Area; Same as the example before, only the fields 'LocationName' and 'LocationArea' will be retrieved from the database, the Anonymous Type can hold as many values you want. Section 3.4: Execute multiple queries async and in parallel When using async queries, you can execute multiple queries at the same time, but not on the same context. If the execution time of one query is 10s, the time for the bad example will be 20s, while the time for the good example will be 10s. Bad Example IEnumerable<TResult1> result1; IEnumerable<TResult2> result2; using(var context = new Context()) { result1 = await context.Set<TResult1>().ToListAsync().ConfigureAwait(false); result2 = await context.Set<TResult1>().ToListAsync().ConfigureAwait(false); } Good Example public async Task<IEnumerable<TResult>> GetResult<TResult>() { using(var context = new Context()) { return await context.Set<TResult1>().ToListAsync().ConfigureAwait(false); } }
GoalKicker.com – Entity Framework Notes for Professionals 13 IEnumerable<TResult1> result1; IEnumerable<TResult2> result2; var result1Task = GetResult<TResult1>(); var result2Task = GetResult<TResult2>(); await Task.WhenAll(result1Task, result2Task).ConfigureAwait(false); var result1 = result1Task.Result; var result2 = result2Task.Result; Section 3.5: Working with stub entities Say we have Products and Categorys in a many-to-many relationship: public class Product { public Product() { Categories = new HashSet<Category>(); } public int ProductId { get; set; } public string ProductName { get; set; } public virtual ICollection<Category> Categories { get; private set; } } public class Category { public Category() { Products = new HashSet<Product>(); } public int CategoryId { get; set; } public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; } } If we want to add a Category to a Product, we have to load the product and add the category to its Categories, for example: Bad Example: var product = db.Products.Find(1); var category = db.Categories.Find(2); product.Categories.Add(category); db.SaveChanges(); (where db is a DbContext subclass). This creates one record in the junction table between Product and Category. However, this table only contains two Id values. It's a waste of resources to load two full entities in order to create one tiny record. A more efficient way is to use stub entities, i.e. entity objects, created in memory, containing only the bare minimum of data, usually only an Id value. This is what it looks like: Good example: // Create two stub entities
GoalKicker.com – Entity Framework Notes for Professionals 14 var product = new Product { ProductId = 1 }; var category = new Category { CategoryId = 2 }; // Attach the stub entities to the context db.Entry(product).State = System.Data.Entity.EntityState.Unchanged; db.Entry(category).State = System.Data.Entity.EntityState.Unchanged; product.Categories.Add(category); db.SaveChanges(); The end result is the same, but it avoids two roundtrips to the database. Prevent duplicates It you want to check if the association already exists, a cheap query suffices. For example: var exists = db.Categories.Any(c => c.Id == 1 && c.Products.Any(p => p.Id == 14)); Again, this won't load full entities into memory. It effectively queries the junction table and only returns a boolean. Section 3.6: Disable change tracking and proxy generation If you just want to get data, but not modify anything, you can turn off change tracking and proxy creation. This will improve your performance and also prevent lazy loading. Bad Example: using(var context = new Context()) { return await context.Set<MyEntity>().ToListAsync().ConfigureAwait(false); } Good Example: using(var context = new Context()) { context.Configuration.AutoDetectChangesEnabled = false; context.Configuration.ProxyCreationEnabled = false; return await context.Set<MyEntity>().ToListAsync().ConfigureAwait(false); } It is particularly common to turn these off from within the constructor of your context, especially if you wish these to be set across your solution: public class MyContext : DbContext { public MyContext() : base("MyContext") { Configuration.AutoDetectChangesEnabled = false; Configuration.ProxyCreationEnabled = false; } //snip }
GoalKicker.com – Entity Framework Notes for Professionals 15 Chapter 4: .t4 templates in entity framework Section 4.1: Dynamically adding Interfaces to model When working with existing model that is quite big and is being regenerated quite often in cases where abstraction needed it might be costly to manually go around redecorating model with interfaces. In such cases one might want to add some dynamic behavior to model generation. Following example will show how automatically add interfaces on classes that have specific column names: In your model go to .tt file modify the EntityClassOpening method in following way, this will add IPolicyNumber interface on entities that have POLICY_NO column, and IUniqueId on UNIQUE_ID public string EntityClassOpening(EntityType entity) { var stringsToMatch = new Dictionary<string,string> { { "POLICY_NO", "IPolicyNumber" }, { "UNIQUE_ID", "IUniqueId" } }; return string.Format( CultureInfo.InvariantCulture, "{0} {1}partial class {2}{3}{4}", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)), stringsToMatch.Any(o => entity.Properties.Any(n => n.Name == o.Key)) ? " : " + string.Join(", ", stringsToMatch.Join(entity.Properties, l => l.Key, r => r.Name, (l,r) => l.Value)) : string.Empty); } This is one specific case but it shows a power of being able to modify .tt templates. Section 4.2: Adding XML Documentation to Entity Classes On every generated model classes there are no documentation comments added by default. If you want to use XML documentation comments for every generated entity classes, find this part inside [modelname].tt (modelname is current EDMX file name): foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection)) { fileManager.StartNewFile(entity.Name + ".cs"); BeginNamespace(code); // used to write model namespace #> <#=codeStringGenerator.UsingDirectives(inHeader: false)#> You can add the XML documentation comments before UsingDirectives line as shown in example below: foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection)) { fileManager.StartNewFile(entity.Name + ".cs"); BeginNamespace(code); #> /// <summary> /// <#=entity.Name#> model entity class. /// </summary>
GoalKicker.com – Entity Framework Notes for Professionals 16 <#=codeStringGenerator.UsingDirectives(inHeader: false)#> The generated documentation comment should be includes entity name as given below. /// <summary> /// Example model entity class. /// </summary> public partial class Example { // model contents }