why I can't query/save data in this way RRS feed

  • Question

  • Hi all,

    I have a question about EF usage. Please help me. 

    1 I create the POCO classes corresponding tables in one Database. The generated source code is that:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.ComponentModel.DataAnnotations;
    namespace Server
    	public class Recorder:DbContext
    		   	public DbSet<register1> register1s {get;set;}
    	    	public DbSet<register2> register2s {get;set;}
    			public class register1
    					public Int32 column1 {get; set;}
    				public String column2 {get; set;}
    	 		public class register2
    					public Int32 column1 {get; set;}
    				public String column2 {get; set;}


    2 Then I generate the assembly in memory by using CShareCodeProvider. Finally use DbContext instance to query Database, but there is a exception in this code line: var t1= d.register1s.Find(1);

    The exception information is that: 

    System.Data.SqlClient.SqlException occurred

      Message=Invalid object name 'dbo.EdmMetadata'.

      Source=.Net SqlClient Data Provider









           at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)





    var results = codeProvider.CompileAssemblyFromSource(param, sourceCode);
          if (results.Errors.Count > 0)
            //A message box shows the erros that occured
            Console.WriteLine("Errors building source code into {0}" + results.PathToAssembly);
            //for each error that occured in the code make a separete message box
            foreach (CompilerError ce in results.Errors)
              Console.WriteLine(" {0}" + ce.ToString());
            return null;
          var compiledAssembly = results.CompiledAssembly;
          var types = compiledAssembly.GetExportedTypes();
          types.ToList().ForEach(t => Console.WriteLine(t.FullName));
          DbContext dbContext = null;
          if (types.Any(t => t.FullName == "Server.Recorder"))
            dbContext = compiledAssembly.CreateInstance("Server.Recorder") as DbContext;
          if (dbContext == null)
            Console.WriteLine("can't get DbContext object");
            return null;
          object registerObj = null;
          if (types.Any(t => t.FullName == "Server.register1"))
            registerObj = compiledAssembly.CreateInstance("Server.register1");
          if (registerObj == null)
            Console.WriteLine("can't create register1 instance");
            return null;
          dynamic d = dbContext;      
          var t1= d.register1s.Find(1);
          if (t1 == null)
            return null;
    Please let me know how to fix this issue. Thanks!



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, December 17, 2010 6:04 AM