문제

이 형식으로 수업을 구축해야합니다

namespace BestCompanyEver
{
    public class PersonInfo
    {
        public string Name{get;set;}
        public int Age{get;set;}
    }
}

열 이름과 나이가있는 테이블 사람에서.

이를 위해 솔루션을 사용할 준비가 되셨습니까?

나는 이것을 T4 또는 Codesmith로 구현할 수 있다는 것을 알고 있지만 이미 그것을 한 사람이 있어야합니다.

도움이 되었습니까?

해결책

나는 내가 사용할 수있는 멋진 T4 템플릿을 발견했다. CodePlex의 프로젝트에서 나온 것입니다.

T4 용 LINQ에서 SQL 템플릿

템플릿을 읽기가 어렵습니다. 단순화하는 데 시간이 걸렸습니다. 사용하기 전에 프로젝트에서 포함 (csharpdataclasses.tt)을 다운로드해야합니다.

다음은 내 템플릿 ()입니다.

        <# // L2ST4 - LINQ to SQL templates for T4 v0.82 - http://www.codeplex.com/l2st4
        // Copyright (c) Microsoft Corporation.  All rights reserved.
        // This source code is made available under the terms of the Microsoft Public License (MS-PL)
        #><#@ template language="C#v3.5" hostspecific="True"
        #><#@ include file="L2ST4.ttinclude"
        #><#@ output extension=".generated.cs"
        #><# // Set options here
        var options = new {
            DbmlFileName = Host.TemplateFile.Replace(".tt",".dbml"), // Which DBML file to operate on (same filename as template)
            SerializeDataContractSP1 = false, // Emit SP1 DataContract serializer attributes
            FilePerEntity = true, // Put each class into a separate file
            StoredProcedureConcurrency = false, // Table updates via an SP require @@rowcount to be returned to enable concurrency
            EntityFilePath = Path.GetDirectoryName(Host.TemplateFile) // Where to put the files 
        };
        var code = new CSharpCodeLanguage();
        var data = new Data(options.DbmlFileName);
        var manager = new Manager(Host, GenerationEnvironment, true) { OutputPath = options.EntityFilePath };
        data.ContextNamespace = (new string[] { manager.GetCustomToolNamespace(data.DbmlFileName), data.SpecifiedContextNamespace, manager.DefaultProjectNamespace }).FirstOrDefault(s => !String.IsNullOrEmpty(s));
        data.EntityNamespace = (new string[] { manager.GetCustomToolNamespace(data.DbmlFileName), data.SpecifiedEntityNamespace, manager.DefaultProjectNamespace }).FirstOrDefault(s => !String.IsNullOrEmpty(s));
        manager.StartHeader();

        manager.EndHeader();
        var tableOperations = new List<TableOperation>();
            foreach(var table in data.Tables)
                tableOperations.AddRange(table.Operations);
            foreach(Table table in data.Tables)
                foreach(OperationType operationType in Enum.GetValues(typeof(OperationType)))
                    if (!tableOperations.Any(o => (o.Table == table) && (o.Type == operationType))) {}
        if (!String.IsNullOrEmpty(data.ContextNamespace)) {}
        foreach(Table table in data.Tables) {
            foreach(TableClass class1 in table.Classes) {
                manager.StartBlock(Path.ChangeExtension(class1.Name + "Info" ,".cs"));
                if (!String.IsNullOrEmpty(data.EntityNamespace)) {#>
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        namespace <#=data.EntityNamespace#>
        {   
        <#      }

        #>  <#=code.Format(class1.TypeAttributes)#> class <#=class1.Name#>Info
            {

        <#      int dataMemberIndex = 1;
                if (class1.Columns.Count > 0) {
        #><#            foreach(Column column in class1.Columns) {#>
                private <#=code.Format(column.StorageType)#> <#= "_" + char.ToLower(column.Member[0]) +  column.Member.Substring(1) #><# if (column.IsReadOnly) {#> = default(<#=code.Format(column.StorageType)#>)<#}#>;

                <#=code.Format(column.MemberAttributes)#><#=code.Format(column.Type)#> <#=column.Member#>
                {
                    get { return <#= "_" + char.ToLower(column.Member[0]) +  column.Member.Substring(1) #>; }
                    set {<#= "_" + char.ToLower(column.Member[0]) +  column.Member.Substring(1) #> = value;}
                }

        <#          }
                }
        #>
            }
        }
        <#      
                manager.EndBlock();
            }
        }
        manager.StartFooter();
        manager.EndFooter(); 
        manager.Process(options.FilePerEntity);#>

다른 팁

또는 LINQ에서 SQL 또는 LLBLGEN PRO

또는 subsonic 또는 nhibernate ...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top