本文整理汇总了C#中DatabaseSchemaReader.DataSchema.DatabaseSchema.AddTable方法的典型用法代码示例。如果您正苦于以下问题:C# DatabaseSchema.AddTable方法的具体用法?C# DatabaseSchema.AddTable怎么用?C# DatabaseSchema.AddTable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DatabaseSchemaReader.DataSchema.DatabaseSchema
的用法示例。
在下文中一共展示了DatabaseSchema.AddTable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrepareModel
private static DatabaseSchema PrepareModel()
{
var schema = new DatabaseSchema(null, null);
schema.AddTable("Categories")
.AddColumn("CategoryId", DbType.Int32).AddPrimaryKey()
.AddColumn("CategoryName", DbType.String);
schema.AddTable("Suppliers")
.AddColumn("SupplierId", DbType.Int32).AddPrimaryKey()
.AddColumn("SupplierName", DbType.String);
schema.AddTable("Products")
.AddColumn("ProductId", DbType.Int32).AddPrimaryKey().AddIdentity()
.AddColumn("ProductName", DbType.String)
.AddColumn("SupplierKey", DbType.Int32).AddForeignKey("fk", "Suppliers");
schema.AddTable("CategoryProducts")
.AddColumn("CategoryId", DbType.Int32).AddPrimaryKey()
.AddForeignKey("fk", "Categories")
.AddColumn("ProductId", DbType.Int32).AddPrimaryKey()
.AddForeignKey("fk", "Products");
DatabaseSchemaFixer.UpdateReferences(schema);
PrepareSchemaNames.Prepare(schema, new Namer());
return schema;
}
示例2: TestSimpleMapping
public void TestSimpleMapping()
{
//arrange
var schema = new DatabaseSchema(null, null);
schema.AddTable("Categories")
.AddColumn("CategoryId", DbType.Int32).AddPrimaryKey()
.AddColumn("CategoryName", DbType.String);
var products = schema.AddTable("Products")
.AddColumn("ProductId", DbType.Int32).AddPrimaryKey()
.AddColumn("ProductName", DbType.String)
.AddColumn("CategoryId", DbType.Int32).AddForeignKey("fk", "Categories")
.Table;
DatabaseSchemaFixer.UpdateReferences(schema);
var settings = new CodeWriterSettings { Namespace = "MyTest", CodeTarget = CodeTarget.PocoNHibernateHbm };
PrepareSchemaNames.Prepare(schema, settings.Namer);
//act
var target = new MappingWriter(products, settings);
var txt = target.Write();
//assert
var errors = Validate(txt);
Assert.IsFalse(errors);
}
示例3: Arrange
private static DatabaseSchema Arrange()
{
var schema = new DatabaseSchema(null, null);
schema.AddTable("Categories")
.AddColumn<int>("CategoryId").AddPrimaryKey()
.AddColumn<string>("CategoryName")
.AddTable("Products")
.AddColumn<int>("ProductId").AddPrimaryKey()
.AddColumn<string>("ProductName");
var assoc =
schema.AddTable("ProductCategories")
.AddColumn<int>("CategoryId").AddPrimaryKey().AddForeignKey("Categories")
.AddColumn<int>("ProductId").AddForeignKey("Products")
.Table;
assoc.PrimaryKey.AddColumn(assoc.FindColumn("ProductId"));
DatabaseSchemaFixer.UpdateDataTypes(schema);
//make sure .Net names are assigned
PrepareSchemaNames.Prepare(schema, new Namer());
return schema;
}
示例4: TestReferencedTableViaConstraintName
public void TestReferencedTableViaConstraintName()
{
//create a schema
var schema = new DatabaseSchema(null, SqlType.SqlServer);
schema.AddTable("Products")
.AddColumn("ProductId").AddPrimaryKey()
.AddColumn("ProductName")
.AddColumn("CategoryId")
.AddTable("Categories")
.AddColumn("CategoryId").AddPrimaryKey("CategoryPK")
.AddColumn("CategoryName")
;
//look at the schema
var categories = schema.FindTableByName("Categories");
var products = schema.FindTableByName("Products");
//from the database we normally get a RefersToTable defined.
//sometimes we don't- we just get the name of the pk constraint
//so here we simulate that
var fk = new DatabaseConstraint
{
ConstraintType = ConstraintType.ForeignKey,
TableName = "Categories",
RefersToConstraint = "CategoryPK"
};
fk.Columns.Add("CategoryId");
products.AddConstraint(fk);
//act
var referencedTable = fk.ReferencedTable(schema);
//assert
Assert.AreEqual(categories, referencedTable);
}
示例5: WriteTest
public void WriteTest()
{
//arrange
var schema = new DatabaseSchema(null, null);
var table = schema.AddTable("Categories")
.AddColumn("CategoryId", "INT").AddPrimaryKey().AddIdentity()
.AddColumn("CategoryName", "NVARCHAR").Table;
//we need datatypes
schema.DataTypes.Add(new DataType("INT", "System.Int32"));
schema.DataTypes.Add(new DataType("NVARCHAR", "System.String"));
DatabaseSchemaFixer.UpdateDataTypes(schema);
//make sure .Net names are assigned
PrepareSchemaNames.Prepare(schema, new Namer());
var cw = new ClassWriter(table, new CodeWriterSettings());
//act
var txt = cw.Write();
//assert
var hasName = txt.Contains("public class Category");
var hasCategoryId = txt.Contains("public virtual int CategoryId { get; set; }");
var hasCategoryName = txt.Contains("public virtual string CategoryName { get; set; }");
Assert.IsTrue(hasName);
Assert.IsTrue(hasCategoryId);
Assert.IsTrue(hasCategoryName);
}
示例6: WriteTest
public void WriteTest()
{
//arrange
var schema = new DatabaseSchema(null, null);
var table = schema.AddTable("Categories")
.AddColumn("CategoryId", "INT").AddPrimaryKey().AddIdentity()
.AddColumn("CategoryName", "NVARCHAR").Table;
//we need datatypes
schema.DataTypes.Add(new DataType("INT", "System.Int32"));
schema.DataTypes.Add(new DataType("NVARCHAR", "System.String"));
DatabaseSchemaFixer.UpdateDataTypes(schema);
//make sure .Net names are assigned
PrepareSchemaNames.Prepare(schema, new Namer());
//inject the custom code inserter
var codeWriterSettings = new CodeWriterSettings {CodeInserter = new CustomCodeInserter()};
var cw = new ClassWriter(table, codeWriterSettings);
//act
var txt = cw.Write();
//assert
Assert.IsTrue(txt.Contains("using System.ComponentModel.DataAnnotations.Schema"));
Assert.IsTrue(txt.Contains("[Table(\"Categories\")]"));
Assert.IsTrue(txt.Contains("[Column(\"CategoryId\")]"));
}
示例7: RemoveTable
public void RemoveTable()
{
//arrange
var schema = new DatabaseSchema(null, null);
schema
.AddTable("Category")
.AddColumn<int>("CategoryId").AddPrimaryKey()
.AddColumn<string>("CategoryName").AddLength(50).AddNullable()
.AddTable("Product")
.AddColumn<int>("Id").AddPrimaryKey()
.AddColumn<int>("CategoryId").AddForeignKey("Category")
.AddTable("Vendor")
.AddColumn<int>("Id").AddPrimaryKey()
.AddColumn<int>("CategoryId").AddForeignKey("Category")
;
//act
schema.RemoveTable("Category");
//assert
Assert.AreEqual(2, schema.Tables.Count);
var prod = schema.FindTableByName("Product");
Assert.AreEqual(0, prod.ForeignKeys.Count);
var categoryId = prod.FindColumn("CategoryId");
Assert.IsFalse(categoryId.IsForeignKey);
}
示例8: DataTypeWithTypeTest
public void DataTypeWithTypeTest()
{
//arrange
var schema = new DatabaseSchema(null, null);
schema
.AddTable("Categories")
.AddColumn<int>("CategoryId").AddPrimaryKey()
.AddColumn<string>("CategoryName").AddLength(50).AddNullable()
.AddColumn<decimal>("StockLevel").AddPrecisionScale(8, 2).AddNullable()
.AddColumn<DateTime>("Updated");
//assert
var cats = schema.FindTableByName("Categories");
var id = cats.PrimaryKeyColumn;
Assert.AreEqual("INT", id.DbDataType);
Assert.AreEqual(true, id.DataType.IsInt);
var catName = cats.FindColumn("CategoryName");
Assert.AreEqual("NVARCHAR", catName.DbDataType);
Assert.AreEqual(true, catName.DataType.IsString);
var stock = cats.FindColumn("StockLevel");
Assert.AreEqual("DECIMAL", stock.DbDataType);
Assert.AreEqual(true, stock.DataType.IsNumeric);
var updated = cats.FindColumn("Updated");
Assert.AreEqual("DATETIME", updated.DbDataType);
Assert.AreEqual(true, updated.DataType.IsDateTime);
}
示例9: TestForeignKeysToUniqueKey
public void TestForeignKeysToUniqueKey()
{
//arrange
var schema = new DatabaseSchema(null, null);
schema.AddTable("Target")
.AddColumn<int>("Target_Id").AddPrimaryKey("TARGET_PK")
.AddColumn<string>("Name").AddLength(10).AddUniqueKey("TARGET_UQ1")
.AddTable("Source")
.AddColumn<int>("Source_Id").AddPrimaryKey("SOURCE_PK")
.AddColumn<string>("Target_Name");
var source = schema.FindTableByName("Source");
var target = schema.FindTableByName("Target");
source.AddConstraint(new DatabaseConstraint
{
ConstraintType = ConstraintType.ForeignKey,
Name = "SOURCE_FK",
RefersToTable = "Target",
TableName = "Source",
RefersToConstraint = "TARGET_UQ1",
});
var fk = source.ForeignKeys[0];
fk.Columns.Add("Target_Name");
//act
var referencedColumns = fk.ReferencedColumns(schema);
//assert
Assert.AreEqual(target.UniqueKeys[0].Columns, referencedColumns, "Columns are matched up");
}
示例10: FindOracleAutoNumberTrigger
public void FindOracleAutoNumberTrigger()
{
//arrange
var schema = new DatabaseSchema(null, SqlType.Oracle);
var table = schema.AddTable("Test");
var id = table.AddColumn<int>("Id").AddPrimaryKey();
id.IsAutoNumber = true;
table.AddColumn<string>("Name").AddLength(200);
var databaseTrigger = new DatabaseTrigger
{
Name = "Test_INS_TRG",
TriggerEvent = "INSERT",
TriggerBody = @"BEGIN
SELECT ""Test_SEQ"".NEXTVAL INTO :NEW.""Id"" FROM DUAL;
END;",
TriggerType = "BEFORE EACH ROW",
};
table.Triggers.Add(databaseTrigger);
var databaseSequence = new DatabaseSequence { IncrementBy = 1, MinimumValue = 0, Name = "Test_SEQ" };
schema.Sequences.Add(databaseSequence);
//act
var result = OracleSequenceTrigger.FindTrigger(table);
//assert
Assert.IsNotNull(result);
Assert.IsNotNull(result.DatabaseTrigger);
Assert.IsNotNull(result.DatabaseSequence);
Assert.AreEqual(databaseSequence, result.DatabaseSequence);
Assert.AreEqual(databaseTrigger, result.DatabaseTrigger);
}
示例11: GivenUnnamedConstraintsThenStandardNamesAssigned
public void GivenUnnamedConstraintsThenStandardNamesAssigned()
{
//arrange
var schema = new DatabaseSchema(null, SqlType.PostgreSql);
schema
.AddTable("Category")
.AddColumn<int>("CategoryId").AddPrimaryKey()
.AddColumn<string>("CategoryName").AddLength(50).AddNullable()
.AddTable("Product")
.AddColumn<int>("Id").AddPrimaryKey()
.AddColumn<string>("ProductName").AddLength(50).AddUniqueKey()
.AddColumn<int>("CategoryId").AddForeignKey("Category")
;
var table = schema.FindTableByName("Product");
var ddlGen = new DdlGeneratorFactory(SqlType.PostgreSql);
var tabGen = ddlGen.TableGenerator(table);
//act
var ddl = tabGen.Write();
//assert
var hasPrimaryKey =
ddl.Contains("ALTER TABLE \"Product\" ADD CONSTRAINT \"Product_Id_pkey\" PRIMARY KEY (\"Id\");");
var hasUniqueKey =
ddl.Contains("ALTER TABLE \"Product\" ADD CONSTRAINT \"Product_ProductName_key\" UNIQUE (\"ProductName\");");
Assert.IsTrue(hasPrimaryKey);
Assert.IsTrue(hasUniqueKey);
}
示例12: TestOracleTableWithTrigger
public void TestOracleTableWithTrigger()
{
//arrange
var schema = new DatabaseSchema(null, SqlType.Oracle);
var table = schema.AddTable("Test");
var id = table.AddColumn<int>("Id").AddPrimaryKey();
id.IsAutoNumber = true;
table.AddColumn<string>("Name").AddLength(200);
table.Triggers.Add(new DatabaseTrigger
{
Name = "Test_INS_TRG",
TriggerEvent = "INSERT",
TriggerBody = @"BEGIN
SELECT ""Test_SEQ"".NEXTVAL INTO :NEW.""Id"" FROM DUAL;
END;",
TriggerType = "BEFORE EACH ROW",
});
var tableGen = new TableGenerator(table);
//act
var ddl = tableGen.Write();
//assert
Assert.IsTrue(ddl.Contains("\"Id\" NUMBER (9) NOT NULL,"), "Table should include Id column " + ddl);
//line breaks may cause environmental differences
Assert.IsTrue(ddl.Contains(@"CREATE OR REPLACE TRIGGER ""Test_INS_TRG"""), "Table should include 'create trigger' " + ddl);
Assert.IsTrue(ddl.Contains(@"SELECT ""Test_SEQ"".NEXTVAL INTO :NEW.""Id"" FROM DUAL;"), "Table should include trigger body " + ddl);
}
示例13: TestNaturalKey
public void TestNaturalKey()
{
//arrange
var schema = new DatabaseSchema(null, null);
var diagrams = schema.AddTable("Diagrams")
.AddColumn("diagram_id", DbType.Int32).AddPrimaryKey()
.AddColumn<string>("name").AddLength(10).AddUniqueKey()
.AddColumn<int>("principal_id")
.Table;
diagrams.UniqueKeys.Single().AddColumn(diagrams.FindColumn("principal_id"));
DatabaseSchemaFixer.UpdateReferences(schema);
var settings = new CodeWriterSettings { Namespace = "MyTest", CodeTarget = CodeTarget.PocoNHibernateHbm };
PrepareSchemaNames.Prepare(schema, settings.Namer);
//act
var target = new MappingWriter(diagrams, settings);
var txt = target.Write();
//assert
var errors = Validate(txt);
Assert.IsFalse(errors);
}
示例14: TestCompositeKey
public void TestCompositeKey()
{
//arrange
var schema = new DatabaseSchema(null, null);
var orderDetail = schema.AddTable("OrderDetails")
.AddColumn("OrderID", DbType.Int32).AddPrimaryKey()
.AddColumn("ProductID", DbType.Int32)
.AddColumn<int>("UnitPrice")
.Table;
orderDetail.PrimaryKey.AddColumn(orderDetail.FindColumn("ProductID"));
DatabaseSchemaFixer.UpdateReferences(schema);
var settings = new CodeWriterSettings { Namespace = "MyTest", CodeTarget = CodeTarget.PocoNHibernateHbm };
PrepareSchemaNames.Prepare(schema, settings.Namer);
//act
var target = new MappingWriter(orderDetail, settings);
var txt = target.Write();
//assert
var errors = Validate(txt);
Assert.IsFalse(errors);
}
示例15: TestForeignKeysCrossSchema
public void TestForeignKeysCrossSchema()
{
//arrange
var schema = new DatabaseSchema(null, null);
schema.AddTable("Target")
.AddColumn<int>("Target_Id").AddPrimaryKey("TARGET_PK")
.AddColumn<string>("Name").AddLength(10).AddUniqueKey("TARGET_UQ1")
.AddTable("Source")
.AddColumn<int>("Source_Id").AddPrimaryKey("SOURCE_PK")
.AddColumn<int>("Target_Id");
var source = schema.FindTableByName("Source");
var target = schema.FindTableByName("Target");
target.SchemaOwner = "dbo";
source.SchemaOwner = "other";
var targetId = source.FindColumn("Target_Id");
targetId.AddForeignKey("SOURCE_FK", tables => target);
//act
var fk = source.ForeignKeys[0];
//assert
Assert.AreEqual("Target", fk.RefersToTable, "Name of referenced table");
Assert.AreEqual(target, fk.ReferencedTable(schema), "Actual reference to table");
Assert.AreEqual(target.PrimaryKey.Columns, fk.ReferencedColumns(schema), "Columns are matched up");
}