Esempio n. 1
0
        public void Create(IEnumerable <IEntity> entities)
        {
            if (entities == null || entities.Count() <= 0)
            {
                throw new ArgumentException("Entity Collection cannot be empty", "entities");
            }

            XMLNamespaceFactory.Version = (byte)_settings.EntityFrameworkVersion;

            // TODO: We need to look into seeing if the following attributes are required in EF6.
            //<edmx:ConceptualModels>
            // <Schema Namespace="SQLModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">

            try {
                //1. Load Database Object from and existing Edmx file or create a new Edmx Object.
                _edmx = File.Exists(_settings.MappingFile) ? Edmx.Load(_settings.MappingFile) : new Edmx();
            } catch (Exception ex) {
                throw new ApplicationException(String.Format("There was an error parsing your edmx file. If you are upgrading your existing templates you will need to delete your edmx file and regenerate. Please contact support for more information. Exception: {0}", ex.Message), ex);
            }

            bool temp;

            if (bool.TryParse(GetDesignerProperty("IncludeForeignKeysInModel"), out temp))
            {
                _includeForeignKeysInModel = temp;
            }

            //<DesignerProperty Name="EnablePluralization" Value="False" />
            //<DesignerProperty Name="CodeGenerationStrategy" Value="None" />

            foreach (var entity in entities)
            {
                if (!entity.HasKey)
                {
                    var message = String.Format("warning 6013: The table/view '{0}' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and regenerate it.", entity.EntityKeyName);
                    Debug.WriteLine(message);
                    Trace.WriteLine(message);
                }
                else if (entity is CommandEntity && IsValidFunction(entity as CommandEntity))
                {
                    var message = String.Format("warning 6005: The function '{0}' has a parameter that has a data type (E.G., 'sql_variant') which is not supported. The function was excluded.", entity.EntityKeyName);
                    Debug.WriteLine(message);
                    Trace.WriteLine(message);
                }
            }

            entities = entities.Where(e => e.HasKey || (e is CommandEntity && IsValidFunction(e as CommandEntity)));

            //2. Sync and create the mapping models.
            //   This also builds up a list of renamed column names that we need to keep track of.
            MergeMappingModel(entities);

            foreach (IEntity entity in entities)
            {
                if (entity is TableEnumEntity)
                {
                    Debug.WriteLine("Getting Enum Table: {0}", entity.EntityKeyName);
                    //GetEnum(entity as TableEnumEntity);
                }
                else if (entity is TableEntity)
                {
                    Debug.WriteLine("Getting Table Schema: {0}", entity.EntityKeyName);
                    GetEntity(entity as TableEntity);
                }
                else if (Configuration.Instance.IncludeViews && entity is ViewEntity)
                {
                    Debug.WriteLine("Getting View Schema: {0}", entity.EntityKeyName);
                    GetEntity(entity as ViewEntity);
                }
                else if (Configuration.Instance.IncludeFunctions && entity is CommandEntity)
                {
                    Debug.WriteLine("Getting Function Schema: {0}", entity.EntityKeyName);
                    GetFunctionEntity(entity as CommandEntity);
                }

                OnSchemaItemProcessed(entity.EntityKeyName);
            }

            foreach (IEntity entity in entities)
            {
                if (entity is TableEntity)
                {
                    CreateStorageAssociations(entity as TableEntity);
                    CreateConceptualAssociations(entity as TableEntity);
                }
            }

            // validate Edmx File
            Validate();
            UpdateDesignerProperites();

            _edmx.Save(_settings.MappingFile);
        }
Esempio n. 2
0
        public void Create(IEnumerable<IEntity> entities)
        {
            if (entities == null || entities.Count() <= 0)
                throw new ArgumentException("Entity Collection cannot be empty", "entities");

            XMLNamespaceFactory.Version = (byte)_settings.EntityFrameworkVersion;

            // TODO: We need to look into seeing if the following attributes are required in EF6.
            //<edmx:ConceptualModels>
            // <Schema Namespace="SQLModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">

            try {
                //1. Load Database Object from and existing Edmx file or create a new Edmx Object.
                _edmx = File.Exists(_settings.MappingFile) ? Edmx.Load(_settings.MappingFile) : new Edmx();
            } catch (Exception ex) {
                throw new ApplicationException(String.Format("There was an error parsing your edmx file. If you are upgrading your existing templates you will need to delete your edmx file and regenerate. Please contact support for more information. Exception: {0}", ex.Message), ex);
            }

            bool temp;
            if (bool.TryParse(GetDesignerProperty("IncludeForeignKeysInModel"), out temp))
                _includeForeignKeysInModel = temp;

            //<DesignerProperty Name="EnablePluralization" Value="False" />
            //<DesignerProperty Name="CodeGenerationStrategy" Value="None" />

            foreach (var entity in entities) {
                if (!entity.HasKey) {
                    var message = String.Format("warning 6013: The table/view '{0}' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and regenerate it.", entity.EntityKeyName);
                    Debug.WriteLine(message);
                    Trace.WriteLine(message);
                } else if (entity is CommandEntity && IsValidFunction(entity as CommandEntity)) {
                    var message = String.Format("warning 6005: The function '{0}' has a parameter that has a data type (E.G., 'sql_variant') which is not supported. The function was excluded.", entity.EntityKeyName);
                    Debug.WriteLine(message);
                    Trace.WriteLine(message);
                }
            }

            entities = entities.Where(e => e.HasKey || (e is CommandEntity && IsValidFunction(e as CommandEntity)));

            //2. Sync and create the mapping models.
            //   This also builds up a list of renamed column names that we need to keep track of.
            MergeMappingModel(entities);

            foreach (IEntity entity in entities) {
                if (entity is TableEnumEntity) {
                    Debug.WriteLine("Getting Enum Table: {0}", entity.EntityKeyName);
                    //GetEnum(entity as TableEnumEntity);
                } else if (entity is TableEntity) {
                    Debug.WriteLine("Getting Table Schema: {0}", entity.EntityKeyName);
                    GetEntity(entity as TableEntity);
                } else if (Configuration.Instance.IncludeViews && entity is ViewEntity) {
                    Debug.WriteLine("Getting View Schema: {0}", entity.EntityKeyName);
                    GetEntity(entity as ViewEntity);
                } else if (Configuration.Instance.IncludeFunctions && entity is CommandEntity) {
                    Debug.WriteLine("Getting Function Schema: {0}", entity.EntityKeyName);
                    GetFunctionEntity(entity as CommandEntity);
                }

                OnSchemaItemProcessed(entity.EntityKeyName);
            }

            foreach (IEntity entity in entities) {
                if (entity is TableEntity) {
                    CreateStorageAssociations(entity as TableEntity);
                    CreateConceptualAssociations(entity as TableEntity);
                }
            }

            // validate Edmx File
            Validate();
            UpdateDesignerProperites();

            _edmx.Save(_settings.MappingFile);
        }