예제 #1
0
        protected virtual ModelDatabaseMapping BuildMapping([NotNull] IModel model)
        {
            Check.NotNull(model, "model");

            // TODO: Consider making this lazy since we don't want to load the whole model just to
            // save changes to a single entity.
            var database = new DatabaseModel();
            var mapping  = new ModelDatabaseMapping(model, database);

            foreach (var entityType in model.EntityTypes)
            {
                var table = BuildTable(database, entityType);
                mapping.Map(entityType, table);

                foreach (var property in OrderProperties(entityType))
                {
                    mapping.Map(property, BuildColumn(table, property));

                    BuildSequence(property, database);
                }

                var primaryKey = entityType.GetPrimaryKey();
                if (primaryKey != null)
                {
                    mapping.Map(primaryKey, BuildPrimaryKey(database, primaryKey));
                }

                foreach (var key in entityType.Keys.Except(new[] { primaryKey }))
                {
                    mapping.Map(key, BuildUniqueConstraint(database, key));
                }

                foreach (var index in entityType.Indexes)
                {
                    mapping.Map(index, BuildIndex(database, index));
                }
            }

            foreach (var entityType in model.EntityTypes)
            {
                foreach (var foreignKey in entityType.ForeignKeys)
                {
                    mapping.Map(foreignKey, BuildForeignKey(database, foreignKey));
                }
            }

            return(mapping);
        }
        public virtual ModelDatabaseMapping GetMapping([NotNull] IModel model)
        {
            Check.NotNull(model, "model");

            return(_mappingCache.GetOrAdd(model, m =>
            {
                // TODO: Consider making this lazy since we don't want to load the whole model just to
                // save changes to a single entity.
                var database = new DatabaseModel();
                var mapping = new ModelDatabaseMapping(m, database);

                foreach (var entityType in m.EntityTypes)
                {
                    var table = BuildTable(database, entityType);
                    mapping.Map(entityType, table);

                    foreach (var property in OrderProperties(entityType))
                    {
                        mapping.Map(property, BuildColumn(table, property));
                    }

                    var primaryKey = entityType.GetPrimaryKey();
                    if (primaryKey != null)
                    {
                        mapping.Map(primaryKey, BuildPrimaryKey(database, primaryKey));
                    }

                    foreach (var index in entityType.Indexes)
                    {
                        mapping.Map(index, BuildIndex(database, index));
                    }
                }

                foreach (var entityType in m.EntityTypes)
                {
                    foreach (var foreignKey in entityType.ForeignKeys)
                    {
                        mapping.Map(foreignKey, BuildForeignKey(database, foreignKey));
                    }
                }

                return mapping;
            }));
        }