tags:

views:

1133

answers:

1

Title says what i'm trying to do. I can successfully generate an assembly if i don't specify the LinqOverTypedDatasets option, but i want my typed DataSet to support queries with LINQ.

My code outputs the error:

error CS0006: Metadata file 'System.Data.DataSetExtensions.dll' could not be found

The code:

//System.Data.DataSet myDataSet = << assume myDataSet is valid DataSet object >>;

Dictionary<string, string> options = new Dictionary<string, string>();
options.Add("CompilerVersion", "v3.5");

using (CSharpCodeProvider cscp = new CSharpCodeProvider(options))
{
    CodeNamespace ns = new CodeNamespace("DBSPPS");
    CodeCompileUnit ccu = new CodeCompileUnit();

    using (StringWriter schemaWriter = new StringWriter())
    {
        myDataSet.WriteXmlSchema(schemaWriter);
        TypedDataSetGenerator.Generate(schemaWriter.ToString(),
            ccu,
            ns,
            cscp,
            TypedDataSetGenerator.GenerateOption.LinqOverTypedDatasets
            );
    }

    StringWriter codeWriter = new StringWriter();

    cscp.GenerateCodeFromNamespace(ns, codeWriter, new CodeGeneratorOptions());

    CompilerParameters parameters = new CompilerParameters();
    parameters.GenerateExecutable = false;
    parameters.OutputAssembly = "DBSPPS.dll";
    parameters.ReferencedAssemblies.Add("System.dll");
    parameters.ReferencedAssemblies.Add("System.Data.dll");
    parameters.ReferencedAssemblies.Add("System.Xml.dll");
    parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");

    CompilerResults cr = cscp.CompileAssemblyFromSource(parameters,new string[]{ codeWriter.ToString() });

    foreach (string msg in cr.Output)
        Console.WriteLine(msg);
}

EDIT: figured this out, corrected code appears above! :) The MSDN documentation is WRONG when it describes the setting the compiler version to 3.5. The value for CompilerVersion should be "v3.5" NOT "3.5" as the docs say.

I got the same compilation errors when i didn't reference required assembiles (System.dll, etc), which was fixed by adding them to the ReferencedAssemblies collection of the CompilerParameters object. However, when i received the error message about System.Data.DataSetExtensions.dll, adding that assembly to the ReferencedAssemblies still resulted in the same error.

I noticed that System.Data.DataSetExtensions.dll isn't in the location i expected it to be (\WINDOWS\Microsoft.NET\Framework\v3.5), but in \Program Files\Reference Assemblies\Microsoft\Framework\v3.5. I tried specifying the full path, that didn't work either. I assumed that since the full path wasn't specified for the other referenced assemblies, it found them in the GAC. Is this not the case for System.Data.DataSetExtensions.dll ? Or is there something else going on?

Thanks for your help.

+1  A: 

Try tweaking the config so it knows about 3.5 - see the config example here.

Marc Gravell
Thanks Marc! See my note in my edited post.
Matt