예제 #1
0
        public Tables GetTables(string classPrefix, string classSuffix, string schemaName)
        {
            _classPrefix = classPrefix;
            _classSuffix = classSuffix;
            _schemaName  = schemaName;

            var _factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            Tables result;

            using (var conn = _factory.CreateConnection())
            {
                conn.ConnectionString = _connectionString;
                conn.Open();

                var reader = new SqlServerSchemaReader();

                result = reader.ReadSchema(conn, _factory);

                // Remove unrequired tables/views
                for (int i = result.Count - 1; i >= 0; i--)
                {
                    if (_schemaName != null && string.Compare(result[i].Schema, _schemaName, true) != 0)
                    {
                        result.RemoveAt(i);
                        continue;
                    }
                    //if (!IncludeViews && result[i].IsView)
                    //{
                    //	result.RemoveAt(i);
                    //	continue;
                    //}
                }

                conn.Close();


                var rxClean = new Regex("^(Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$");
                foreach (var t in result)
                {
                    t.ClassName = _classPrefix + t.ClassName + _classSuffix;
                    foreach (var c in t.Columns)
                    {
                        c.PropertyName = rxClean.Replace(c.PropertyName, "_$1");

                        // Make sure property name doesn't clash with class name
                        if (c.PropertyName == t.ClassName)
                        {
                            c.PropertyName = "_" + c.PropertyName;
                        }
                    }
                }

                return(result);
            }
        }
        private Tables LoadTables(DbProviderFactory factory)
        {
            if (factory == null || !(Settings.ElementsToGenerate.HasFlag(Elements.Poco) ||
                                     Settings.ElementsToGenerate.HasFlag(Elements.Context) ||
                                     Settings.ElementsToGenerate.HasFlag(Elements.UnitOfWork) ||
                                     Settings.ElementsToGenerate.HasFlag(Elements.PocoConfiguration)))
            {
                return(new Tables());
            }

            try
            {
                using (var conn = factory.CreateConnection())
                {
                    if (conn == null)
                    {
                        return(new Tables());
                    }

                    conn.ConnectionString = Settings.ConnectionString;
                    conn.Open();

                    var reader = new SqlServerSchemaReader(conn, factory);
                    var tables = reader.ReadSchema();
                    var fkList = reader.ReadForeignKeys();
                    reader.IdentifyForeignKeys(fkList, tables);

                    foreach (var t in tables)
                    {
                        if (Settings.UseDataAnnotations || Settings.UseDataAnnotationsWithFluent)
                        {
                            t.SetupDataAnnotations();
                        }
                        t.Suffix = Settings.TableSuffix;
                    }

                    Settings.AddForeignKeys?.Invoke(fkList, tables);

                    // Work out if there are any foreign key relationship naming clashes
                    reader.ProcessForeignKeys(fkList, tables, true);

                    tables.IdentifyMappingTables(fkList, Settings.UseMappingTables);

                    // Now we know our foreign key relationships and have worked out if there are any name clashes,
                    // re-map again with intelligently named relationships.
                    tables.ResetNavigationProperties();

                    reader.ProcessForeignKeys(fkList, tables, false);

                    tables.IdentifyMappingTables(fkList, !Settings.UseMappingTables);

                    conn.Close();
                    return(tables);
                }
            }
            catch (Exception x)
            {
                PrintError("Failed to read database schema in LoadTables().", x);
                return(new Tables());
            }
        }
예제 #3
0
		public Tables GetTables(string classPrefix, string classSuffix, string schemaName)
		{
			_classPrefix = classPrefix;
			_classSuffix = classSuffix;
			_schemaName = schemaName;

			var _factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
		
			Tables result;
			using (var conn=_factory.CreateConnection())
			{
				conn.ConnectionString = _connectionString;         
				conn.Open();
		
				var reader= new SqlServerSchemaReader();
		
				result = reader.ReadSchema(conn, _factory);
		
				// Remove unrequired tables/views
				for (int i=result.Count-1; i>=0; i--)
				{
					if (_schemaName != null && string.Compare(result[i].Schema, _schemaName, true) != 0)
					{
						result.RemoveAt(i);
						continue;
					}
					//if (!IncludeViews && result[i].IsView)
					//{
					//	result.RemoveAt(i);
					//	continue;
					//}
				}

				conn.Close();

		
				var rxClean = new Regex("^(Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$");
				foreach (var t in result)
				{
					t.ClassName = _classPrefix + t.ClassName + _classSuffix;
					foreach (var c in t.Columns)
					{
						c.PropertyName = rxClean.Replace(c.PropertyName, "_$1");

						// Make sure property name doesn't clash with class name
						if (c.PropertyName == t.ClassName)
							c.PropertyName = "_" + c.PropertyName;
					}
				}

				return result;
			}
		}