本文整理汇总了C#中System.Data.Entity.Migrations.Model.AddPrimaryKeyOperation类的典型用法代码示例。如果您正苦于以下问题:C# AddPrimaryKeyOperation类的具体用法?C# AddPrimaryKeyOperation怎么用?C# AddPrimaryKeyOperation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AddPrimaryKeyOperation类属于System.Data.Entity.Migrations.Model命名空间,在下文中一共展示了AddPrimaryKeyOperation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Can_get_default_for_name
public void Can_get_default_for_name()
{
var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = "T" };
Assert.Equal("PK_T", addPrimaryKeyOperation.Name);
Assert.True(addPrimaryKeyOperation.HasDefaultName);
}
示例2: Can_set_and_get_primary_key
public void Can_set_and_get_primary_key()
{
var addPrimaryKeyOperation = new AddPrimaryKeyOperation();
var table = new CreateTableOperation("T") { PrimaryKey = addPrimaryKeyOperation };
Assert.Same(addPrimaryKeyOperation, table.PrimaryKey);
Assert.Equal("T", addPrimaryKeyOperation.Table);
}
示例3: DefaultName_is_restricted_to_128_chars
public void DefaultName_is_restricted_to_128_chars()
{
var addPrimaryKeyOperation = new AddPrimaryKeyOperation
{
Table = new string('t', 150)
};
Assert.Equal(128, addPrimaryKeyOperation.DefaultName.Length);
}
示例4: Can_get_and_set_table_and_name_and_columns
public void Can_get_and_set_table_and_name_and_columns()
{
var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = "T", Name = "Pk" };
addPrimaryKeyOperation.Columns.Add("pk2");
Assert.Equal("T", addPrimaryKeyOperation.Table);
Assert.Equal("Pk", addPrimaryKeyOperation.Name);
Assert.Equal("pk2", addPrimaryKeyOperation.Columns.Single());
Assert.False(addPrimaryKeyOperation.HasDefaultName);
}
示例5: Inverse_should_return_drop_operation
public void Inverse_should_return_drop_operation()
{
var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = "T", Name = "Pk" };
addPrimaryKeyOperation.Columns.Add("pk2");
var inverse = (DropPrimaryKeyOperation)addPrimaryKeyOperation.Inverse;
Assert.Equal("T", inverse.Table);
Assert.Equal("Pk", inverse.Name);
Assert.Equal("pk2", inverse.Columns.Single());
}
示例6: Generate
protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation)
{
if (addPrimaryKeyOperation == null)
{
throw new ArgumentNullException("addPrimaryKeyOperation");
}
// turn off clustered index for all PKs (except for migration history)
if (!addPrimaryKeyOperation.Table.Contains("__MigrationHistory"))
{
addPrimaryKeyOperation.IsClustered = false;
}
base.Generate(addPrimaryKeyOperation);
}
示例7: Convert
private void Convert(AddPrimaryKeyOperation addPrimaryKeyOperation)
{
StringBuilder sql = new StringBuilder();
sql.Append("ALTER TABLE ");
AppendTableName(addPrimaryKeyOperation.Table, sql);
sql.Append(" ADD CONSTRAINT \"");
sql.Append(addPrimaryKeyOperation.Name);
sql.Append("\" PRIMARY KEY ");
sql.Append("(");
foreach (var column in addPrimaryKeyOperation.Columns)
{
sql.Append('"');
sql.Append(column);
sql.Append("\",");
}
sql.Remove(sql.Length - 1, 1);
sql.Append(")");
AddStatment(sql);
}
示例8: GetUpgradeOperations
public virtual IEnumerable<MigrationOperation> GetUpgradeOperations()
{
if (!Exists())
{
yield break;
}
DbConnection connection = null;
try
{
connection = CreateConnection();
var tableName = "dbo." + HistoryContext.DefaultTableName;
DbProviderManifest providerManifest;
if (connection.GetProviderInfo(out providerManifest).IsSqlCe())
{
tableName = HistoryContext.DefaultTableName;
}
using (var context = new LegacyHistoryContext(connection))
{
var createdOnExists = false;
try
{
InjectInterceptionContext(context);
using (new TransactionScope(TransactionScopeOption.Suppress))
{
context.History
.Select(h => h.CreatedOn)
.FirstOrDefault();
}
createdOnExists = true;
}
catch (EntityException)
{
}
if (createdOnExists)
{
yield return new DropColumnOperation(tableName, "CreatedOn");
}
}
using (var context = CreateContext(connection))
{
if (!_contextKeyColumnExists)
{
if (_historyContextFactory != HistoryContext.DefaultFactory)
{
throw Error.UnableToUpgradeHistoryWhenCustomFactory();
}
yield return new AddColumnOperation(
tableName,
new ColumnModel(PrimitiveTypeKind.String)
{
MaxLength = _contextKeyMaxLength,
Name = "ContextKey",
IsNullable = false,
DefaultValue = _contextKey
});
var emptyModel = new DbModelBuilder().Build(connection).GetModel();
var createTableOperation = (CreateTableOperation)
new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single();
var dropPrimaryKeyOperation
= new DropPrimaryKeyOperation
{
Table = tableName,
CreateTableOperation = createTableOperation
};
dropPrimaryKeyOperation.Columns.Add("MigrationId");
yield return dropPrimaryKeyOperation;
yield return new AlterColumnOperation(
tableName,
new ColumnModel(PrimitiveTypeKind.String)
{
MaxLength = _migrationIdMaxLength,
Name = "MigrationId",
IsNullable = false
},
isDestructiveChange: false);
var addPrimaryKeyOperation
= new AddPrimaryKeyOperation
{
Table = tableName
};
addPrimaryKeyOperation.Columns.Add("MigrationId");
addPrimaryKeyOperation.Columns.Add("ContextKey");
//.........这里部分代码省略.........
示例9: Create_operations_to_upgrade_primary_key_of_history_table
private static MigrationOperation[] Create_operations_to_upgrade_primary_key_of_history_table()
{
const string tableName = "dbo." + HistoryContext.DefaultTableName;
CreateTableOperation createTableOperation;
using (var context = new HistoryContext())
{
var emptyModel = new DbModelBuilder()
.Build(context.Database.Connection).GetModel();
createTableOperation = (CreateTableOperation)
new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single();
}
var addColumnOperation =
new AddColumnOperation(
tableName,
new ColumnModel(PrimitiveTypeKind.String)
{
MaxLength = 512,
Name = "ContextKey",
IsNullable = false,
DefaultValue = "DefaultContextKey"
});
var dropPrimaryKeyOperation
= new DropPrimaryKeyOperation
{
Table = tableName,
CreateTableOperation = createTableOperation
};
dropPrimaryKeyOperation.Columns.Add("MigrationId");
var alterColumnOperation
= new AlterColumnOperation(
tableName,
new ColumnModel(PrimitiveTypeKind.String)
{
MaxLength = 150,
Name = "MigrationId",
IsNullable = false
},
isDestructiveChange: false);
var addPrimaryKeyOperation
= new AddPrimaryKeyOperation
{
Table = tableName
};
addPrimaryKeyOperation.Columns.Add("MigrationId");
addPrimaryKeyOperation.Columns.Add("ContextKey");
return
new MigrationOperation[]
{
addColumnOperation,
dropPrimaryKeyOperation,
alterColumnOperation,
addPrimaryKeyOperation
};
}
示例10: Generate_can_output_add_primary_key_operation
public void Generate_can_output_add_primary_key_operation()
{
var migrationProvider = new SqlServerMigrationSqlGenerator();
var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = "T" };
addPrimaryKeyOperation.Columns.Add("c1");
addPrimaryKeyOperation.Columns.Add("c2");
var sql = migrationProvider.Generate(new[] { addPrimaryKeyOperation }, "2008").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE [T] ADD CONSTRAINT [PK_T] PRIMARY KEY ([c1], [c2])"));
}
示例11: BuildChangePrimaryKeyOperations
private static IEnumerable<PrimaryKeyOperation> BuildChangePrimaryKeyOperations(
string oldTable, string newTable, XElement oldKey, XElement newKey)
{
var dropPrimaryKeyOperation = new DropPrimaryKeyOperation
{
Table = oldTable
};
oldKey.Descendants(EdmXNames.Ssdl.PropertyRefNames).Each(
pr => dropPrimaryKeyOperation.Columns.Add(pr.NameAttribute()));
yield return dropPrimaryKeyOperation;
var addPrimaryKeyOperation = new AddPrimaryKeyOperation
{
Table = newTable
};
newKey.Descendants(EdmXNames.Ssdl.PropertyRefNames).Each(
pr => addPrimaryKeyOperation.Columns.Add(pr.NameAttribute()));
yield return addPrimaryKeyOperation;
}
示例12: BuildCreateTableOperation
private CreateTableOperation BuildCreateTableOperation(
string entitySetName,
string tableName,
string schema,
string isSystem,
ModelMetadata modelMetadata)
{
Contract.Requires(!string.IsNullOrWhiteSpace(entitySetName));
Contract.Requires(!string.IsNullOrWhiteSpace(tableName));
Contract.Requires(!string.IsNullOrWhiteSpace(schema));
Contract.Requires(modelMetadata != null);
var createTableOperation = new CreateTableOperation(GetQualifiedTableName(tableName, schema));
var entityTypeElement = modelMetadata.Model.Descendants(EdmXNames.Ssdl.EntityTypeNames)
.Single(et => et.NameAttribute().EqualsIgnoreCase(entitySetName));
entityTypeElement
.Descendants(EdmXNames.Ssdl.PropertyNames)
.Each(p => createTableOperation.Columns.Add(BuildColumnModel(p, entitySetName, modelMetadata)));
var addPrimaryKeyOperation = new AddPrimaryKeyOperation();
entityTypeElement
.Descendants(EdmXNames.Ssdl.PropertyRefNames)
.Each(pr => addPrimaryKeyOperation.Columns.Add(pr.NameAttribute()));
createTableOperation.PrimaryKey = addPrimaryKeyOperation;
createTableOperation.IsSystem = isSystem.EqualsIgnoreCase("true");
return createTableOperation;
}
示例13: Generate
/// <summary>
/// Generates code to perform an <see cref = "AddPrimaryKeyOperation" />.
/// </summary>
/// <param name = "addPrimaryKeyOperation">The operation to generate code for.</param>
/// <param name = "writer">Text writer to add the generated code to.</param>
protected virtual void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation, IndentedTextWriter writer)
{
Contract.Requires(addPrimaryKeyOperation != null);
Contract.Requires(writer != null);
writer.Write("AddPrimaryKey(");
writer.Write(Quote(addPrimaryKeyOperation.Table));
writer.Write(", ");
var compositeIndex = addPrimaryKeyOperation.Columns.Count() > 1;
if (compositeIndex)
{
writer.Write("New String() { ");
}
writer.Write(addPrimaryKeyOperation.Columns.Join(Quote));
if (compositeIndex)
{
writer.Write(" }");
}
if (!addPrimaryKeyOperation.HasDefaultName)
{
writer.Write(", name := ");
writer.Write(Quote(addPrimaryKeyOperation.Name));
}
writer.WriteLine(")");
}
示例14: Generate
protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation, IndentedTextWriter writer)
{
addPrimaryKeyOperation.Table = TrimSchemaPrefix(addPrimaryKeyOperation.Table);
base.Generate(addPrimaryKeyOperation, writer);
}
示例15: Generate
/// <summary>
/// Generates SQL for a <see cref="AddPrimaryKeyOperation" />.
/// Generated SQL should be added using the Statement method.
/// </summary>
/// <param name="addPrimaryKeyOperation"> The operation to produce SQL for. </param>
protected virtual void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation)
{
Check.NotNull(addPrimaryKeyOperation, "addPrimaryKeyOperation");
using (var writer = Writer())
{
writer.Write("ALTER TABLE ");
writer.Write(Name(addPrimaryKeyOperation.Table));
writer.Write(" ADD CONSTRAINT ");
writer.Write(Quote(addPrimaryKeyOperation.Name));
writer.Write(" PRIMARY KEY ");
if (!addPrimaryKeyOperation.IsClustered)
{
writer.Write("NONCLUSTERED ");
}
writer.Write("(");
writer.Write(addPrimaryKeyOperation.Columns.Select(Quote).Join());
writer.Write(")");
Statement(writer);
}
}