| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  | <%@ Template Language="C#" TargetLanguage="C#" Debug="True" Encoding="UTF-8" %> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  | <%@ Assembly Src="../Internal/Model.cs" %> | 
					
						
							|  |  |  | <%@ Assembly Src="../Internal/Extensions.cs" %> | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <%@ Import Namespace="System.Collections.Generic" %> | 
					
						
							|  |  |  | <%@ Import Namespace="System.Linq" %> | 
					
						
							|  |  |  | <%@ Import Namespace="System.Text" %> | 
					
						
							|  |  |  | <%@ Import Namespace="System.Text.RegularExpressions" %> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <%@ Import Namespace="SchemaMapper" %> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  | <%@ Property Name="WholeDb"  | 
					
						
							|  |  |  | Type="System.Boolean"  | 
					
						
							|  |  |  | Category="1.Database" | 
					
						
							|  |  |  | Default="true" | 
					
						
							|  |  |  | Description="是否为整个数据库" %> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <%@ Property Name="SourceDatabase" | 
					
						
							|  |  |  |     Type="SchemaExplorer.DatabaseSchema" | 
					
						
							|  |  |  |     Category="1.Database" | 
					
						
							|  |  |  |     Description="The source database." %> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <%@ Property Name="SourceTables"  | 
					
						
							|  |  |  | Type="SchemaExplorer.TableSchemaCollection"  | 
					
						
							|  |  |  | Category="1.Database" Description="选择部分表" %> | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <%@ Property Name="ContextNamespace" Type="System.String"  %> | 
					
						
							|  |  |  | <%@ Property Name="EntityNamespace" Type="System.String"  %> | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |               | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  | using Microsoft.EntityFrameworkCore; | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  | using <%= EntityNamespace %>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace <%= ContextNamespace %> | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |     <% | 
					
						
							|  |  |  |     string dbContextName; | 
					
						
							|  |  |  |         if(WholeDb){ | 
					
						
							|  |  |  |           dbContextName = SourceDatabase.Name.ToSafeName(); | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |         else{ | 
					
						
							|  |  |  |             dbContextName = SourceTables.First().Database.Name.ToSafeName(); | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  |         dbContextName = StringUtil.ToPascalCase(dbContextName); | 
					
						
							|  |  |  |        Response.WriteLine("   public partial class "+ dbContextName +"Context: DbContext"); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |     %> | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |         protected override void OnModelCreating(ModelBuilder modelBuilder) | 
					
						
							| 
									
										
										
										
											2017-11-29 20:49:14 +08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |             //当主键为联合主键时,需要把这里的内容拷贝到对应的位置 | 
					
						
							|  |  |  |             <% | 
					
						
							|  |  |  |             TableSchemaCollection tables; | 
					
						
							|  |  |  |             if(WholeDb){ | 
					
						
							|  |  |  |                 tables = SourceDatabase.Tables; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else{ | 
					
						
							|  |  |  |                 tables = SourceTables; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach(TableSchema table in tables) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if(table.PrimaryKeys.Count <=1) continue; | 
					
						
							|  |  |  |                 var keys = string.Join(",", table.Columns.Where(u=>u.IsPrimaryKeyMember==true) | 
					
						
							|  |  |  |                 .Select(u =>"c."+u.Name)); | 
					
						
							|  |  |  |                 Response.WriteLine("        modelBuilder.Entity<"+table.Name+">()"); | 
					
						
							|  |  |  |                 Response.WriteLine("        .HasKey(c => new { "+keys+" });"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             %> | 
					
						
							| 
									
										
										
										
											2017-11-29 20:49:14 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |         <% | 
					
						
							|  |  |  |         foreach(TableSchema table in tables) | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |             Response.WriteLine("        public virtual DbSet<"+table.Name+"> "+StringUtil.ToPascalCase(StringUtil.ToPlural(table.Name))+" { get; set; }"); | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-22 14:59:36 +08:00
										 |  |  |         %> | 
					
						
							| 
									
										
										
										
											2017-11-29 18:26:36 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-07 11:47:43 +08:00
										 |  |  | } |