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); }
internal static void AddArtifactReference(AssemblyBuilder assemblyBuilder, BuildProvider prov, string virtualPath) { // add the artifact as a resource to the DLL using (Stream input = VirtualPathProvider.OpenFile(virtualPath)) { // derive the resource name string name = BuildProviderUtils.GetResourceNameForVirtualPath(virtualPath); using (Stream resStream = assemblyBuilder.CreateEmbeddedResource(prov, name)) { int byteRead = input.ReadByte(); while (byteRead != -1) { resStream.WriteByte((byte)byteRead); byteRead = input.ReadByte(); } } } }
/// <summary> /// Extract the CSDL, SSDL and MSL nodes from the EDMX file and store them /// as embedded resources /// </summary> /// <param name="assemblyBuilder"></param> public override void GenerateCode(AssemblyBuilder assemblyBuilder) { using (StreamReader edmxInputStream = new StreamReader(VirtualPathProvider.OpenFile(base.VirtualPath))) { // load up an XML document representing the edmx file XmlElement conceptualSchemaElement; XmlElement mappingElement; XmlElement storageSchemaElement; string embedAsResourcePropertyValue; EntityDesignerUtils.ExtractConceptualMappingAndStorageNodes(edmxInputStream, out conceptualSchemaElement, out mappingElement, out storageSchemaElement, out embedAsResourcePropertyValue); if (null == conceptualSchemaElement) { throw new XmlException("No Conceptual Schema node to embed as a resource", null, 0, 0); } if (null == storageSchemaElement) { throw new XmlException("No Storage Schema node to embed as a resource", null, 0, 0); } if (null == mappingElement) { throw new XmlException("No Mapping node to embed as a resource", null, 0, 0); } // construct output paths where the CSDL/MSL/SSDL resources will be placed string virtualPathPrefix = base.VirtualPath.Replace(EntityDesignerUtils._edmxFileExtension, String.Empty); string csdlResourceName = BuildProviderUtils.GetResourceNameForVirtualPath(virtualPathPrefix + XmlConstants.CSpaceSchemaExtension); string ssdlResourceName = BuildProviderUtils.GetResourceNameForVirtualPath(virtualPathPrefix + XmlConstants.SSpaceSchemaExtension); string mslResourceName = BuildProviderUtils.GetResourceNameForVirtualPath(virtualPathPrefix + XmlConstants.CSSpaceSchemaExtension); SetupEmbeddedResource(assemblyBuilder, this, conceptualSchemaElement, csdlResourceName); SetupEmbeddedResource(assemblyBuilder, this, storageSchemaElement, ssdlResourceName); SetupEmbeddedResource(assemblyBuilder, this, mappingElement, mslResourceName); } }
/// <summary> /// /// </summary> /// <param name="assemblyBuilder"></param> public override void GenerateCode(AssemblyBuilder assemblyBuilder) { BuildProviderUtils.AddArtifactReference(assemblyBuilder, this, base.VirtualPath); }