MSBuild, custom task to run custom tool to generate classes for linq to sql model?

前端 未结 1 1884

I have the following scenario. We use stored procedures to access the database and we use LiNQ 2 SQL to generate the classes or namely we use Unplugged LINQ to SQL Generator

1条回答
  •  时光取名叫无心
    2021-01-03 11:35

    You haven't declared an output property in your task. You have to use the Output attribute on OutputFiles property.

    public class GenerateDesignerDC : Task
    {
        [Required]
        public ITaskItem[] InputFiles { get; set; }
    
        [Output]
        public ITaskItem[] OutputFiles { get; set; }
    
        public override bool Execute()
        {
            var generatedFileNames = new List();
            foreach (var task in InputFiles)
            {
    
                string inputFileName = task.ItemSpec;
                string outputFileName = Path.ChangeExtension(inputFileName, ".Designer.cs");
                string result;
    
                // Build code string
                var generator = new ULinqCodeGenerator("CSharp");
                string fileContent;
                using (FileStream fs = File.OpenRead(inputFileName))
                using (StreamReader rd = new StreamReader(fs))
                {
                    fileContent = rd.ReadToEnd();
                }
    
                using (var destination = new FileStream(outputFileName, FileMode.Create))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(generator.BuildCode(inputFileName, fileContent));
                    destination.Write(bytes, 0, bytes.Length);
                }
                generatedFileNames.Add(outputFileName);
            }
    
            OutputFiles = generatedFileNames.Select(name => new TaskItem(name)).ToArray();
    
            return true;
        }
    }
    

    0 讨论(0)
提交回复
热议问题