1 Reply Latest reply: May 4, 2013 10:15 AM by 1007107 RSS

    Managed beta 2 EntityStoreSchemaGenerator

    839514
      Hi,

      I need to generate entity framework edmx file programmatically. I'm using managed beta2 version 4.112.3.60.
      But managed driver generates strange error. "An item with the same key has already been added."
      GenerateStoreMetadata with filters generates this error. Using without filters successfully generates store metadata.

      Here is the sample program :

      class ModelGenerator2
      {
      private bool IsAnyErrorExists(IList<EdmSchemaError> ssdlErrors)
      {
      if (ssdlErrors == null)
      return false;

      foreach (var edmSchemaError in ssdlErrors)
      {
      if (edmSchemaError.Severity == EdmSchemaErrorSeverity.Error)
      {
      return true;
      }
      }

      return false;
      }

      private void WriteErrors(IEnumerable<EdmSchemaError> errors)
      {
      if (errors != null)
      {
      foreach (EdmSchemaError e in errors)
      {
      WriteError(e);
      }
      }
      }

      private void WriteError(EdmSchemaError e)
      {
      if (e.Severity == EdmSchemaErrorSeverity.Error)
      {
      Console.Write("Error: ");
      }
      else
      {
      Console.Write("Warning: ");
      }

      Console.WriteLine(e.Message);
      }


      internal void GenerateModel()
      {

      var filterEntryList = new List<EntityStoreSchemaFilterEntry>();
      filterEntryList.Add(new EntityStoreSchemaFilterEntry(null, "SCOTT", "DEPT"));
      filterEntryList.Add(new EntityStoreSchemaFilterEntry(null, "SCOTT", "EMP"));

      var connectionString = ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString;

      var entityStoreSchemaGenerator = new EntityStoreSchemaGenerator(Constants.ProviderInvariantName
      , connectionString
      , Constants.StoreName);

      entityStoreSchemaGenerator.GenerateForeignKeyProperties = true;
      var ssdlErrors = entityStoreSchemaGenerator.GenerateStoreMetadata(filterEntryList);

      if ((ssdlErrors != null && ssdlErrors.Count > 0))
      {
      WriteErrors(ssdlErrors);
      }

      if (IsAnyErrorExists(ssdlErrors))
      {
      Console.ReadKey();
      return;
      }

      var entityModelSchemaGenerator =
      new EntityModelSchemaGenerator(
      entityStoreSchemaGenerator.EntityContainer, Constants.ModelName, Constants.CsdlEntityContainerName );
      var csdlAndMslErrors = entityModelSchemaGenerator.GenerateMetadata();

      // write out errors
      if (csdlAndMslErrors != null && csdlAndMslErrors.Count > 0)
      {
      System.Console.WriteLine("Errors occurred during generation:");
      WriteErrors(csdlAndMslErrors);
      return;
      }

      }

      }