예제 #1
0
        public ModelContext LoadModel(ModelDeclaration declaration)
        {
            var result = new EntityModelRoot();

            var tableName = declaration.GetParameter("tableName");

            var databaseInfo = new DatabaseInfo
            {
                Server   = declaration.GetParameter("server"),
                Name     = declaration.GetParameter("db"),
                User     = declaration.GetParameter("user"),
                Password = declaration.GetParameter("password")
            };

            var query = TableMetadataQuery;

            AppTrace.Verbose($"Loading metadata from database {databaseInfo.Name} on {databaseInfo.Server} instance.");

            if (!string.IsNullOrWhiteSpace(tableName))
            {
                query = string.Concat(query, " and tbl.TABLE_NAME = @TableName");
            }

            using (var connection = new SqlConnection(SqlHelper.GetConnectionString(databaseInfo)))
            {
                var lines   = connection.Query <DbObjectLine>(TableMetadataQuery, new { TableName = tableName });
                var grouped = lines.GroupBy(l => l.ObjectName);
                foreach (var g in grouped)
                {
                    var m = new EntityModel
                    {
                        ObjectName = SafeName(g.Key)
                    };

                    m.AddProperties(g.OrderBy(p => p.PropertyPosition).Select(p => new PropertyModel
                    {
                        DateTimePrecision      = p.DateTimePrecision,
                        PropertyDefaultValue   = p.PropertyDefaultValue,
                        PropertyLength         = p.PropertyLength,
                        PropertyName           = p.PropertyName,
                        PropertyPosition       = p.PropertyPosition,
                        PropertyPrecision      = p.PropertyPrecision,
                        PropertyPrecisionRadix = p.PropertyPrecisionRadix,
                        PropertySqlDataType    = p.PropertySqlDataType,
                        Scale         = p.Scale,
                        SqlIsNullable = p.SqlIsNullable
                    }));

                    result.AddModels(new[] { m });
                }
            }

            AppTrace.Verbose($"Loaded { result.Entities.Count() } models.");

            return(new ModelContext(declaration, new DbModel(result, ModelFormat.Clr, declaration.ModelName, declaration.Tags)));
        }
예제 #2
0
 public DbModel(EntityModelRoot root, ModelFormat format, string name, IEnumerable <string> tags) : base(format, name, tags)
 {
     Root = root;
 }