public void GenerateCode(FileProjectItem item, CustomToolContext context)
        {
            LanguageOption languageToGenerateCode = LanguageOption.GenerateCSharpCode;

            if (item.Project.Language != "C#")
                languageToGenerateCode = LanguageOption.GenerateVBCode;

            XDocument edmxDocument = XDocument.Load(item.FileName);
            XElement conceptualModelsElement = EDMXIO.ReadSection(edmxDocument, EDMXIO.EDMXSection.CSDL);

            if (conceptualModelsElement == null)
                throw new ArgumentException("Input file is not a valid EDMX file.");

            XDocument csdlDocument = new XDocument(new XDeclaration("1.0", "utf-8", null), conceptualModelsElement.Element(XName.Get("Schema", csdlNamespace.NamespaceName)));
            
            string tempFileName = IO.GetTempFilenameWithExtension("csdl");
            csdlDocument.Save(tempFileName);

            string outputFileName = context.GetOutputFileName(item, "Designer");

            EntityCodeGenerator entityCodeGenerator = new EntityCodeGenerator(languageToGenerateCode);
            IList<EdmSchemaError> edmSchemaErrors = entityCodeGenerator.GenerateCode(tempFileName, outputFileName);
            File.Delete(tempFileName);
            
            context.EnsureOutputFileIsInProject(item, outputFileName);
        }
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            // look at the assembly builder to see which language we should use in the App_Code directory
            EntityCodeGenerator generator = null;
            if (assemblyBuilder.CodeDomProvider.FileExtension.ToLowerInvariant() == "cs")
            {
                generator = new EntityCodeGenerator(LanguageOption.GenerateCSharpCode);
            }
            else
            {
                generator = new EntityCodeGenerator(LanguageOption.GenerateVBCode);
            }

            // generate the code for our CSDL file
            IList<EdmSchemaError> errors = null;
            using (XmlReader input = XmlReader.Create(VirtualPathProvider.OpenFile(base.VirtualPath)))
            {
                using (StringWriter output = new StringWriter(CultureInfo.InvariantCulture))
                {
                    // Read from input and generate into output, put errors in a class member
                    var entityFrameworkVersion = GetEntityFrameworkVersion(BuildManager.TargetFramework.Version);
                    errors = generator.GenerateCode(input, output, entityFrameworkVersion);
                    if (errors.Count == 0)
                    {
                        output.Flush();
                        assemblyBuilder.AddCodeCompileUnit(this, new CodeSnippetCompileUnit(output.ToString()));
                    }
                }
            }

            // if there are errors, package this data into XmlExceptions and throw this
            // if we are in VS, the ASP .NET stack will place this information in the error pane
            // if we are in the ASP .NET runtime, it will use this information to build the error page
            if (errors != null && errors.Count > 0)
            {
                XmlException inner = null;
                XmlException outer = null;
                foreach (EdmSchemaError error in errors)
                {
                    outer = new XmlException(error.Message, inner, error.Line, error.Column);
                    inner = outer;
                }

                throw outer;
            }

            BuildProviderUtils.AddArtifactReference(assemblyBuilder, this, base.VirtualPath);
        }
		public void GenerateCode(FileProjectItem item, CustomToolContext context)
		{
			XElement schema = GetModelSchema(item);
			XDocument csdlDocument = CreateCsdlDocument(schema);
			
			string tempFileName = IO.GetTempFilenameWithExtension("csdl");
			csdlDocument.Save(tempFileName);

			LanguageOption languageToGenerateCode = GetLanguageOption(item);
			string outputFileName = context.GetOutputFileName(item, "Designer");

			EntityCodeGenerator entityCodeGenerator = new EntityCodeGenerator(languageToGenerateCode);
			AddNamespaceMapping(entityCodeGenerator, schema, context.OutputNamespace);
			IList<EdmSchemaError> edmSchemaErrors = entityCodeGenerator.GenerateCode(tempFileName, outputFileName);
			File.Delete(tempFileName);
			
			context.EnsureOutputFileIsInProject(item, outputFileName);
		}
Exemple #4
0
        public static bool CodeGen(String edmxFile, LanguageOption languageOption, out String codeOut, out List<Object> errors)
        {
            codeOut = String.Empty;

            XDocument xdoc = XDocument.Load(edmxFile);
            XElement c = GetCsdlFromEdmx(xdoc);
            Version v = _namespaceManager.GetVersionFromEDMXDocument(xdoc);

            StringWriter sw = new StringWriter();

            errors = new List<Object>();

            //
            // code-gen uses different classes for V1 and V2 of the EF
            //
            if (v == EntityFrameworkVersions.Version1)
            {
                // generate code
                EntityClassGenerator codeGen = new EntityClassGenerator(languageOption);
                errors = codeGen.GenerateCode(c.CreateReader(), sw) as List<Object>;
            }
            else if (v == EntityFrameworkVersions.Version2)
            {
                EntityCodeGenerator codeGen = new EntityCodeGenerator(languageOption);
                errors = codeGen.GenerateCode(c.CreateReader(), sw) as List<Object>;
            }

            else if (v == EntityFrameworkVersions.Version3)
            {
                EntityCodeGenerator codeGen = new EntityCodeGenerator(languageOption);
                errors = codeGen.GenerateCode(c.CreateReader(), sw) as List<Object>;
            }

            // output errors
            if (errors != null)
                return true;

            codeOut = sw.ToString();
            return false;
        }
Exemple #5
0
 /// <summary>
 ///     This API supports the Entity Framework infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="languageOption">This API supports the Entity Framework infrastructure and is not intended to be used directly from your code.</param>
 /// <param name="targetEntityFrameworkVersion">This API supports the Entity Framework infrastructure and is not intended to be used directly from your code.</param>
 public EntityCodeGenerator(LanguageOption languageOption, Version targetEntityFrameworkVersion)
 {
     _entityCodeGenerator          = new Sded.EntityCodeGenerator((Sded.LanguageOption)languageOption);
     _targetEntityFrameworkVersion = targetEntityFrameworkVersion;
 }
		void AddNamespaceMapping(EntityCodeGenerator entityCodeGenerator, XAttribute edmNamespace, string outputNamespace)
		{
			if (edmNamespace != null)
				entityCodeGenerator.EdmToObjectNamespaceMap.Add(edmNamespace.Value, outputNamespace);
		}
		void AddNamespaceMapping(EntityCodeGenerator entityCodeGenerator, XElement schema, string outputNamespace)
		{
			if (!String.IsNullOrEmpty(outputNamespace))
				AddNamespaceMapping(entityCodeGenerator, schema.Attribute(XName.Get("Namespace")), outputNamespace);
		}
Exemple #8
0
		private void GenerateCode()
		{
			CSDLTemplate csdl = new CSDLTemplate(_model);
			XDocument document = XDocument.Parse(csdl.FileContent);
			XElement c = EdmGen2.GetCsdlFromEdmx(document, EntityFrameworkVersions.Version2);

			// generate code
			StringWriter sw = new StringWriter();
			EntityCodeGenerator codeGen = new EntityCodeGenerator(LanguageOption.GenerateCSharpCode);
			IList<EdmSchemaError> errors = codeGen.GenerateCode(c.CreateReader(), sw);
			if (errors.Count != 0)
			{
				//throw new Exception("The Entity Framework generation failed. Please email [email protected].");
				StringBuilder sb = new StringBuilder();
				foreach (EdmSchemaError error in errors)
				{
				  sb.AppendFormat("error.Column:{0} error.ErrorCode:{1} error.Line:{2} error.Message:{3} error.SchemaLocation:{4} error.SchemaName:{5} error.Severity:{6} error.StackTrace: {7}", error.Column, error.ErrorCode, error.Line, error.Message, error.SchemaLocation, error.SchemaName, error.Severity, error.StackTrace).AppendLine();
				}
				System.Diagnostics.Debug.WriteLine(sb.ToString());
			}

		}
Exemple #9
0
		private static void CodeGen(FileInfo edmxFile, LanguageOption languageOption)
		{
			XDocument xdoc = XDocument.Load(edmxFile.FullName);
			Version v = _namespaceManager.GetVersionFromEDMXDocument(xdoc);
			XElement c = GetCsdlFromEdmx(xdoc);

			StringWriter sw = new StringWriter();
			IList<EdmSchemaError> errors = null;

			//
			// code-gen uses different classes for V1 and V2 of the EF 
			//
			if (v == EntityFrameworkVersions.Version1)
			{
				// generate code
				EntityClassGenerator codeGen = new EntityClassGenerator(languageOption);
				errors = codeGen.GenerateCode(c.CreateReader(), sw);
			}
			else if (v == EntityFrameworkVersions.Version2)
			{
				EntityCodeGenerator codeGen = new EntityCodeGenerator(languageOption);
				errors = codeGen.GenerateCode(c.CreateReader(), sw);
			}

			// write out code-file
			string outputFileName = GetFileNameWithNewExtension(edmxFile,
					GetFileExtensionForLanguageOption(languageOption));
			File.WriteAllText(outputFileName, sw.ToString());

			// output errors
			WriteErrors(errors);
		}
 /// <summary>
 ///     This API supports the Entity Framework infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="languageOption">This API supports the Entity Framework infrastructure and is not intended to be used directly from your code.</param>
 /// <param name="targetEntityFrameworkVersion">This API supports the Entity Framework infrastructure and is not intended to be used directly from your code.</param>
 public EntityCodeGenerator(LanguageOption languageOption, Version targetEntityFrameworkVersion)
 {
     _entityCodeGenerator = new Sded.EntityCodeGenerator((Sded.LanguageOption)languageOption);
     _targetEntityFrameworkVersion = targetEntityFrameworkVersion;
 }