/// <summary>
        /// Create the template output
        /// </summary>
        public override string TransformText()
        {
            this.Write("namespace ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextNamespace));
            this.Write("\r\n{\r\n    using ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ConfigurationContextFolder));
            this.Write(";\r\n    using Microsoft.EntityFrameworkCore;\r\n\r\n    /// <summary>\r\n    /// ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextName));
            this.Write("\r\n    /// </summary>\r\n    /// <inheritdoc />\r\n    public partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextName));
            this.Write(" : DbContext\r\n    {\r\n        /// <inheritdoc />\r\n        protected ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextName));
            this.Write("()\r\n        {\r\n        }\r\n\r\n        /// <inheritdoc />\r\n        public ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextName));
            this.Write("(DbContextOptions<");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextName));
            this.Write("> options)\r\n            : base(options)\r\n        {\r\n        }\r\n\r\n");

            foreach (var entityType in Model.GetEntityTypes())
            {
                var tableName = entityType.GetTableName();
                var isView    = entityType.FindAnnotation("Relational:ViewDefinition") != null;

                this.Write("        /// <summary>\r\n        /// ");
                this.Write(this.ToStringHelper.ToStringWithCulture(entityType.Name));
                this.Write(this.ToStringHelper.ToStringWithCulture(isView ? string.Empty : ("(" + tableName + ")")));
                this.Write("\r\n        /// </summary>\r\n        public virtual DbSet<");
                this.Write(this.ToStringHelper.ToStringWithCulture(GetEntityClassName(entityType)));
                this.Write("> ");
                this.Write(this.ToStringHelper.ToStringWithCulture(GetEntityName(entityType)));
                this.Write(" { get; set; }\r\n\r\n");
            }


            this.Write(@"        /// <inheritdoc />
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#if DEBUG
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder");
            this.Write(this.ToStringHelper.ToStringWithCulture(Code.Fragment(ProviderCode.GenerateUseProvider(ConnectionString))));
            this.Write(";\r\n#endif\r\n            }\r\n        }\r\n\r\n        /// <inheritdoc />\r\n        protec" +
                       "ted override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n");

            foreach (var entityType in Model.GetEntityTypes())
            {
                this.Write("            modelBuilder.ApplyConfiguration(new ");
                this.Write(this.ToStringHelper.ToStringWithCulture(GetEntityConfigurationName(entityType)));
                this.Write("());\r\n");
            }

            this.Write("\r\n            OnModelCreatingPartial(modelBuilder);\r\n        }\r\n\r\n        partial" +
                       " void OnModelCreatingPartial(ModelBuilder modelBuilder);\r\n    }\r\n}\r\n");
            return(this.GenerationEnvironment.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public override string TransformText()
        {
            this.Write("using Microsoft.EntityFrameworkCore;\r\nusing ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ModelNamespace));
            this.Write(";\r\n\r\nnamespace ");
            this.Write(this.ToStringHelper.ToStringWithCulture(Namespace));
            this.Write("\r\n{\r\n    public partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ContextName));
            this.Write(" : DbContext\r\n    {\r\n");

            foreach (var entityType in Model.GetEntityTypes())
            {
                this.Write("        public virtual DbSet<");
                this.Write(this.ToStringHelper.ToStringWithCulture(entityType.Name));
                this.Write("> ");
                this.Write(this.ToStringHelper.ToStringWithCulture(entityType["Scaffolding:DbSetName"]));
                this.Write(" { get; set; }\r\n");
            }


            this.Write("\r\n        protected override void OnConfiguring(DbContextOptionsBuilder options)\r" +
                       "\n            => options");
            this.Write(this.ToStringHelper.ToStringWithCulture(Code.Fragment(ProviderCode.GenerateUseProvider(ConnectionString))));
            this.Write(";\r\n\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n" +
                       "        {\r\n");

            foreach (var entityType in Model.GetEntityTypes())
            {
                this.Write("            modelBuilder.ApplyConfiguration(new ");
                this.Write(this.ToStringHelper.ToStringWithCulture(entityType.Name));
                this.Write("Configuration());\r\n");
            }

            this.Write("        }\r\n    }\r\n}\r\n");
            return(this.GenerationEnvironment.ToString());
        }