本文整理汇总了C#中DbLinq.Schema.Dbml.Database类的典型用法代码示例。如果您正苦于以下问题:C# Database类的具体用法?C# Database怎么用?C# Database使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Database类属于DbLinq.Schema.Dbml命名空间,在下文中一共展示了Database类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteClass
protected virtual void WriteClass(CodeWriter writer, Table table, Database schema, GenerationContext context)
{
writer.WriteLine();
string entityBase = context.Parameters.EntityBase;
if (string.IsNullOrEmpty(entityBase))
entityBase = schema.EntityBase;
var specifications = SpecificationDefinition.Partial;
if (table.Type.AccessModifierSpecified)
specifications |= GetSpecificationDefinition(table.Type.AccessModifier);
else
specifications |= SpecificationDefinition.Public;
if (table.Type.ModifierSpecified)
specifications |= GetSpecificationDefinition(table.Type.Modifier);
var tableAttribute = NewAttributeDefinition<TableAttribute>();
tableAttribute["Name"] = table.Name;
using (writer.WriteAttribute(tableAttribute))
using (writer.WriteClass(specifications,
table.Type.Name, entityBase, context.Parameters.EntityInterfaces))
{
WriteClassHeader(writer, table, context);
WriteCustomTypes(writer, table, schema, context);
WriteClassExtensibilityDeclarations(writer, table, context);
WriteClassProperties(writer, table, context);
if (context.Parameters.GenerateEqualsHash)
WriteClassEqualsAndHash(writer, table, context);
WriteClassChildren(writer, table, schema, context);
WriteClassParents(writer, table, schema, context);
WriteClassChildrenAttachment(writer, table, schema, context);
WriteClassCtor(writer, table, schema, context);
}
}
示例2: CheckNames
protected virtual void CheckNames(Database schema)
{
CheckNames(schema,
column => "Contents",
association => association.ThisKey + association.Member,
association => association.Member + association.Type);
}
示例3: WriteDataContextCtors
protected virtual void WriteDataContextCtors(CodeWriter writer, Database schema, Type contextBaseType, GenerationContext context)
{
// ctor taking a connections tring
WriteDataContextCtor(writer, schema, contextBaseType,
new[] { new ParameterDefinition { Name = "connectionString", Type = typeof(string) } },
new[] { "connectionString" },
new[] { typeof(string) },
context);
// the two constructors below have the same prototype, so they are mutually exclusive
// the base class requires a IVendor
if (!WriteDataContextCtor(writer, schema, contextBaseType,
new[] { new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) } },
new[] { "connection", writer.GetNewExpression(writer.GetMethodCallExpression(writer.GetLiteralFullType(context.SchemaLoader.Vendor.GetType()))) },
new[] { typeof(IDbConnection), typeof(IVendor) },
context))
{
// OR the base class requires no IVendor
WriteDataContextCtor(writer, schema, contextBaseType,
new[] { new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) } },
new[] { "connection" },
new[] { typeof(IDbConnection) },
context);
}
// ctor(string, MappingSource)
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(string) },
new ParameterDefinition { Name = "mappingSource", Type = typeof(MappingSource) },
},
new[] { "connection", "mappingSource" },
new[] { typeof(string), typeof (MappingSource) },
context);
// ctor(IDbConnection, MappingSource)
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) },
new ParameterDefinition { Name = "mappingSource", Type = typeof(MappingSource) },
},
new[] { "connection", "mappingSource" },
new[] { typeof(IDbConnection), typeof(MappingSource) },
context);
// just in case you'd like to specify another vendor than the one who helped generating this file
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) },
new ParameterDefinition { Name = "vendor", Type = typeof(IVendor) },
},
new[] { "connection", "vendor" },
new[] { typeof(IDbConnection), typeof(IVendor) },
context);
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) },
new ParameterDefinition { Name = "mappingSource", Type = typeof(MappingSource) },
new ParameterDefinition { Name = "vendor", Type = typeof(IVendor) },
},
new[] { "connection", "mappingSource", "vendor" },
new[] { typeof(IDbConnection), typeof(MappingSource), typeof(IVendor) },
context);
}
示例4: LoadConstraints
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names)
{
var constraints = ReadConstraints(conn, schemaName.DbName);
//sort tables - parents first (this is moving to SchemaPostprocess)
//TableSorter.Sort(tables, constraints);
foreach (DataConstraint keyColRow in constraints)
{
//find my table:
string fullKeyDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema);
DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => fullKeyDbName == t.Name);
if (table == null)
{
bool ignoreCase = true;
table = schema.Tables.FirstOrDefault(t => 0 == string.Compare(fullKeyDbName, t.Name, ignoreCase));
if (table == null)
{
WriteErrorLine("ERROR L46: Table '" + keyColRow.TableName + "' not found for column " + keyColRow.ColumnName);
continue;
}
}
bool isForeignKey = keyColRow.ConstraintName != "PRIMARY"
&& keyColRow.ReferencedTableName != null;
if (isForeignKey)
{
LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName, keyColRow.TableSchema,
keyColRow.ReferencedColumnName, keyColRow.ReferencedTableName, keyColRow.ReferencedTableSchema,
keyColRow.ConstraintName, nameFormat, names);
}
}
}
示例5: GenerateVisualBasic
/// <summary>
/// Generates a Visual Basic source code wrapper for the database schema objects.
/// </summary>
/// <param name="database"></param>
/// <param name="filename"></param>
public void GenerateVisualBasic(Database database, string filename)
{
using (Stream stream = File.Open(filename, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
new VBCodeProvider().CreateGenerator(writer).GenerateCodeFromNamespace(GenerateCodeDomModel(database), writer, new CodeGeneratorOptions() { BracingStyle = "C" });
}
}
}
示例6: Write
public void Write(TextWriter textWriter, Database dbSchema, GenerationContext context)
{
Context = context;
Provider.CreateGenerator(textWriter).GenerateCodeFromNamespace(
GenerateCodeDomModel(dbSchema), textWriter,
new CodeGeneratorOptions() {
BracingStyle = "C",
IndentString = "\t",
});
}
示例7: LoadConstraints
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names)
{
//TableSorter.Sort(tables, constraints); //sort tables - parents first
var constraints = ReadConstraints(conn, schemaName.DbName);
var allKeys2 = ReadForeignConstraints(conn, schemaName.DbName);
var foreignKeys = allKeys2.Where(k => k.ConstraintType == "FOREIGN KEY").ToList();
var primaryKeys = allKeys2.Where(k => k.ConstraintType == "PRIMARY KEY").ToList();
foreach (DataConstraint keyColRow in constraints)
{
//find my table:
string constraintFullDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema);
DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => constraintFullDbName == t.Name);
if (table == null)
{
WriteErrorLine("ERROR L138: Table '" + keyColRow.TableName + "' not found for column " + keyColRow.ColumnName);
continue;
}
//todo: must understand better how PKEYs are encoded.
//In Sasha's DB, they don't end with "_pkey", you need to rely on ReadForeignConstraints().
//In Northwind, they do end with "_pkey".
bool isPrimaryKey = keyColRow.ConstraintName.EndsWith("_pkey")
|| primaryKeys.Count(k => k.ConstraintName == keyColRow.ConstraintName) == 1;
if (isPrimaryKey)
{
//A) add primary key
DbLinq.Schema.Dbml.Column primaryKeyCol = table.Type.Columns.First(c => c.Name == keyColRow.ColumnName);
primaryKeyCol.IsPrimaryKey = true;
}
else
{
DataForeignConstraint dataForeignConstraint = foreignKeys.FirstOrDefault(f => f.ConstraintName == keyColRow.ConstraintName);
if (dataForeignConstraint == null)
{
string msg = "Missing data from 'constraint_column_usage' for foreign key " + keyColRow.ConstraintName;
WriteErrorLine(msg);
//throw new ApplicationException(msg);
continue; //as per Andrus, do not throw. //putting together an Adnrus_DB test case.
}
LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName, keyColRow.TableSchema,
dataForeignConstraint.ColumnName, dataForeignConstraint.ReferencedTableName,
dataForeignConstraint.ReferencedTableSchema,
keyColRow.ConstraintName, nameFormat, names);
}
}
}
示例8: WriteClasses
protected virtual void WriteClasses(CodeWriter writer, Database schema, GenerationContext context)
{
IEnumerable<Table> tables = schema.Tables;
var types = context.Parameters.GenerateTypes;
if (types.Count > 0)
tables = tables.Where(t => types.Contains(t.Type.Name));
foreach (var table in tables)
WriteClass(writer, table, schema, context);
}
示例9: LoadForeignKey
/// <summary>
/// Loads the foreign key.
/// </summary>
/// <param name="schema">The schema.</param>
/// <param name="table">The table.</param>
/// <param name="columnName">Name of the column.</param>
/// <param name="tableName">Name of the table.</param>
/// <param name="tableSchema">The table schema.</param>
/// <param name="referencedColumnName">Name of the referenced column.</param>
/// <param name="referencedTableName">Name of the referenced table.</param>
/// <param name="referencedTableSchema">The referenced table schema.</param>
/// <param name="constraintName">Name of the constraint.</param>
/// <param name="nameFormat">The name format.</param>
/// <param name="names">The names.</param>
protected virtual void LoadForeignKey(Database schema, Table table, string columnName, string tableName, string tableSchema,
string referencedColumnName, string referencedTableName, string referencedTableSchema,
string constraintName,
NameFormat nameFormat, Names names)
{
var foreignKey = BuildForeignKey(names.ColumnsNames[tableName], columnName);
var reverseForeignKey = BuildForeignKey(names.ColumnsNames[referencedTableName], referencedColumnName);
var associationName = CreateAssociationName(tableName, tableSchema,
referencedTableName, referencedTableSchema, constraintName, foreignKey, nameFormat);
//both parent and child table get an [Association]
var assoc = new Association();
assoc.IsForeignKey = true;
assoc.Name = constraintName;
assoc.Type = null;
assoc.ThisKey = foreignKey;
assoc.OtherKey = reverseForeignKey;
assoc.Member = associationName.ManyToOneMemberName;
assoc.CardinalitySpecified = false;
// TODO: generate assoc.Cardinality?
table.Type.Associations.Add(assoc);
//and insert the reverse association:
var reverseAssociation = new Association();
reverseAssociation.Name = constraintName;
reverseAssociation.Type = table.Type.Name;
reverseAssociation.Member = associationName.OneToManyMemberName;
reverseAssociation.CardinalitySpecified = false;
// TODO: generate reverseAssociation.Cardinality?
reverseAssociation.ThisKey = reverseForeignKey;
reverseAssociation.OtherKey = foreignKey;
reverseAssociation.DeleteRule = "NO ACTION";
string referencedFullDbName = GetFullDbName(referencedTableName, referencedTableSchema);
var referencedTable = schema.Tables.FirstOrDefault(t => referencedFullDbName == t.Name);
if (referencedTable == null)
{
//try case-insensitive match
//reason: MySql's Key_Column_Usage table contains both 'Northwind' and 'northwind'
referencedTable = schema.Tables.FirstOrDefault(t => referencedFullDbName.ToLower() == t.Name.ToLower());
}
if (referencedTable == null)
{
ReportForeignKeyError(schema, referencedFullDbName);
}
else
{
referencedTable.Type.Associations.Add(reverseAssociation);
assoc.Type = referencedTable.Type.Name;
}
}
示例10: LoadConstraints
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names)
{
var constraints = ReadConstraints(conn, schemaName.DbName);
foreach (DataConstraint constraint in constraints)
{
//find my table:
string constraintFullDbName = GetFullDbName(constraint.TableName, constraint.TableSchema);
DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => constraintFullDbName == t.Name);
if (table == null)
{
WriteErrorLine("ERROR L100: Table '" + constraint.TableName + "' not found for column " + constraint.ColumnNameList);
continue;
}
//if (table.Name.StartsWith("E"))
// Logger.Write("---Dbg");
if (constraint.ConstraintType == "P")
{
//A) add primary key
DbLinq.Schema.Dbml.Column pkColumn = table.Type.Columns.Where(c => constraint.ColumnNames.Contains(c.Name)).First();
pkColumn.IsPrimaryKey = true;
}
else if (constraint.ConstraintType == "R")
{
//if not PRIMARY, it's a foreign key. (constraint_type=="R")
//both parent and child table get an [Association]
DataConstraint referencedConstraint = constraints.FirstOrDefault(c => c.ConstraintName == constraint.ReverseConstraintName);
if (constraint.ReverseConstraintName == null || referencedConstraint == null)
{
WriteErrorLine("ERROR L127: given R_contraint_name='" + constraint.ReverseConstraintName + "', unable to find parent constraint");
continue;
}
LoadForeignKey(schema, table, constraint.ColumnNameList, constraint.TableName, constraint.TableSchema,
referencedConstraint.ColumnNameList, referencedConstraint.TableName,
referencedConstraint.TableSchema,
constraint.ConstraintName, nameFormat, names);
}
// custom type, this is a trigger
else if (constraint.ConstraintType == "T" && constraint.ColumnNames.Count == 1)
{
var column = table.Type.Columns.Where(c => c.Name == constraint.ColumnNames[0]).First();
column.Expression = constraint.Expression;
column.IsDbGenerated = true;
}
}
//GuessSequencePopulatedFields(schema);
}
示例11: LoadConstraints
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names)
{
//TableSorter.Sort(tables, constraints); //sort tables - parents first
var foreignKeys = ReadConstraints(conn, schemaName.DbName);
foreach (DataConstraint keyColRow in foreignKeys)
{
//find my table:
string constraintFullDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema);
DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => constraintFullDbName == t.Name);
if (table == null)
{
WriteErrorLine("ERROR L138: Table '"
+ keyColRow.TableName
+ "' not found for column "
+ keyColRow.ColumnName);
continue;
}
if (keyColRow.ConstraintType.Equals("P")) //'PRIMARY KEY'
{
//foreach (string pk_name in keyColRow.column_name_primaries)
//{
DbLinq.Schema.Dbml.Column primaryKeyCol = table.Type.Columns.First(c => c.Name == keyColRow.ColumnName);
primaryKeyCol.IsPrimaryKey = true;
//}
continue;
}
if (keyColRow.ConstraintType.Equals("R")) //'FOREIGN KEY'
{
// This is very bad...
if (!names.ColumnsNames[keyColRow.ReferencedTableName].ContainsKey(keyColRow.ReferencedColumnName))
continue;
LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName,
keyColRow.TableSchema,
keyColRow.ReferencedColumnName, keyColRow.ReferencedTableName,
keyColRow.ReferencedTableSchema,
keyColRow.ConstraintName, nameFormat, names);
}
}
}
示例12: LoadStoredProcedures
protected override void LoadStoredProcedures(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat)
{
var parameters = ReadParameters(conn, schemaName.DbName);
foreach (var parameter in parameters)
{
var procedureName = CreateProcedureName(parameter.ProcedureName, parameter.Schema, nameFormat);
Function function = schema.Functions.SingleOrDefault(f => f.Method == procedureName.MethodName);
if (function == null)
{
function = new Function { Name = procedureName.DbName, Method = procedureName.MethodName };
schema.Functions.Add(function);
}
if (parameter.Name == null)
{
var returnParameter = new Return();
returnParameter.DbType = parameter.Type.SqlType;
returnParameter.Type = MapDbType(parameter.Name, parameter.Type).ToString();
function.IsComposable = true;
function.Return = returnParameter;
}
else
{
var functionParameter = new Parameter();
functionParameter.DbType = parameter.Type.SqlType;
functionParameter.Type = MapDbType(parameter.Name, parameter.Type).ToString();
if (parameter.In)
{
if (parameter.Out)
functionParameter.Direction = DbLinq.Schema.Dbml.ParameterDirection.InOut;
else
functionParameter.Direction = DbLinq.Schema.Dbml.ParameterDirection.In;
}
else
functionParameter.Direction = DbLinq.Schema.Dbml.ParameterDirection.Out;
var parameterName = CreateParameterName(parameter.Name, nameFormat);
functionParameter.Name = parameterName.CallName;
function.Parameters.Add(functionParameter);
}
}
}
示例13: CheckConstraintsName
/// <summary>
/// Checks for name conflicts, given lambdas to correct on conflicts
/// </summary>
/// <param name="schema"></param>
/// <param name="tableNamedAssociationRenamer"></param>
/// <param name="columnNamedAssociationRenamer"></param>
protected virtual void CheckConstraintsName(Database schema,
Func<Association, string> tableNamedAssociationRenamer,
Func<Association, string> columnNamedAssociationRenamer)
{
foreach (var table in schema.Tables)
{
foreach (var association in table.Type.Associations)
{
var localAssociation = association;
if (association.Member == table.Type.Name)
association.Member = tableNamedAssociationRenamer(association);
else if ((from column in table.Type.Columns where column.Member == localAssociation.Member select column).FirstOrDefault() != null)
{
association.Member = columnNamedAssociationRenamer(association);
}
}
}
}
示例14: WriteDataContextCtor
protected virtual bool WriteDataContextCtor(CodeWriter writer, Database schema, Type contextBaseType,
ParameterDefinition[] parameters, string[] baseCallParameterNames, Type[] baseCallParameterTypes,
GenerationContext context)
{
// if we have a contextBaseType, then check that we can do it
if (contextBaseType != null)
{
var ctor = contextBaseType.GetConstructor(baseCallParameterTypes);
if (ctor == null)
return false;
}
using (writer.WriteCtor(SpecificationDefinition.Public, schema.Class, parameters, baseCallParameterNames))
{
writer.WriteLine(writer.GetStatement(writer.GetMethodCallExpression("OnCreated")));
}
writer.WriteLine();
return true;
}
示例15: Load
public virtual Database Load(string databaseName, INameAliases nameAliases, NameFormat nameFormat,
bool loadStoredProcedures, string contextNamespace, string entityNamespace)
{
// check if connection is open. Note: we may use something more flexible
if (Connection.State != ConnectionState.Open)
Connection.Open();
// get the database name. If we don't have one, take it from connection string...
if (string.IsNullOrEmpty(databaseName))
databaseName = Connection.Database;
// ... and if connection string doesn't provide a name, then throw an error
if (string.IsNullOrEmpty(databaseName))
throw new ArgumentException("A database name is required. Please specify /database=<databaseName>");
var schemaName = NameFormatter.GetSchemaName(databaseName, GetExtraction(databaseName), nameFormat);
var names = new Names();
var schema = new Database
{
Name = schemaName.DbName,
Class = schemaName.ClassName,
BaseType = typeof(DataContext).FullName,
ContextNamespace = contextNamespace,
EntityNamespace = entityNamespace,
};
// order is important, we must have:
// 1. tables
// 2. columns
// 3. constraints
LoadTables(schema, schemaName, Connection, nameAliases, nameFormat, names);
LoadColumns(schema, schemaName, Connection, nameAliases, nameFormat, names);
LoadConstraints(schema, schemaName, Connection, nameFormat, names);
if (loadStoredProcedures)
LoadStoredProcedures(schema, schemaName, Connection, nameFormat);
CheckNamesCaseSafety(schema);
// check for duplicate names between properties
CheckNames(schema);
// generate backing fields name (since we have here correct names)
GenerateStorageFields(schema);
return schema;
}