Context.tt 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <#@ template hostspecific="true" language="C#" #>
  2. <#@ include file="EF.Utility.CS.ttinclude" #><#@
  3. output extension=".cs" #><#
  4. var efHost = (EfTextTemplateHost)Host;
  5. var code = new CodeGenerationTools(this);
  6. #>
  7. using System.Data.Entity;
  8. using System.Data.Entity.Infrastructure;
  9. using CommonTool;
  10. using <#= code.EscapeNamespace(efHost.MappingNamespace) #>;
  11. namespace <#= code.EscapeNamespace(efHost.Namespace) #>
  12. {
  13. public partial class <#= efHost.EntityContainer.Name #> : DbContext
  14. {
  15. static <#= efHost.EntityContainer.Name #>()
  16. {
  17. Database.SetInitializer<<#= efHost.EntityContainer.Name #>>(null);
  18. }
  19. public <#= efHost.EntityContainer.Name #>()
  20. : base(new GetConnStr().ConnStr)
  21. {
  22. }
  23. <#
  24. foreach (var set in efHost.EntityContainer.BaseEntitySets.OfType<EntitySet>())
  25. {
  26. #>
  27. public DbSet<<#= set.ElementType.Name #>> <#= set.Name #> { get; set; }
  28. <#
  29. }
  30. #>
  31. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  32. {
  33. <#
  34. foreach (var set in efHost.EntityContainer.BaseEntitySets.OfType<EntitySet>())
  35. {
  36. #>
  37. modelBuilder.Configurations.Add(new <#= set.ElementType.Name #>Map());
  38. <#
  39. }
  40. #>
  41. }
  42. }
  43. }