本文整理汇总了C#中EntityMetadata.Select方法的典型用法代码示例。如果您正苦于以下问题:C# EntityMetadata.Select方法的具体用法?C# EntityMetadata.Select怎么用?C# EntityMetadata.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EntityMetadata
的用法示例。
在下文中一共展示了EntityMetadata.Select方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SqlServerGenerator
public SqlServerGenerator(Type entityType)
{
var metadata = new EntityMetadata(entityType);
IsIdentity = metadata.FirstOrDefault(x => x.IsIdentity) != null;
var selectFields = string.Join(", ", metadata.Select(i => $"{i.DbName} AS {i.Name}"));
var insertFields = string.Join(", ", metadata.Where(i => !i.IsIdentity).Select(i => i.DbName));
var insertValues = string.Join(", ", metadata.Where(i => !i.IsIdentity).Select(i => $"@{i.Name}"));
var updatePairs = string.Join(", ", metadata.Where(i => !i.IsPrimaryKey).Select(i => $"{i.DbName} = @{i.Name}"));
var whereCondition = string.Join(" AND ", metadata.Where(i => i.IsPrimaryKey).Select(i => $"{i.DbName} = @{i.Name}"));
SelectAllSql = $"SELECT {selectFields} FROM {metadata.DbName}";
SelectSql = SelectAllSql;
if (whereCondition.Length > 0)
SelectSql += $" WHERE {whereCondition}";
InsertSql = $"INSERT INTO {metadata.DbName} ({insertFields}) VALUES ({insertValues})";
if (IsIdentity)
{
InsertSql += Environment.NewLine;
InsertSql += "SELECT SCOPE_IDENTITY()";
}
UpdateSql = $"UPDATE {metadata.DbName} SET {updatePairs}";
if (whereCondition.Length > 0)
UpdateSql += $" WHERE {whereCondition}";
DeleteAllSql = $"DELETE FROM {metadata.DbName}";
DeleteSql = DeleteAllSql;
if (whereCondition.Length > 0)
DeleteSql += $" WHERE {whereCondition}";
CountSql = $"SELECT COUNT(1) FROM {metadata.DbName}";
}