Add JoinString Demo

This commit is contained in:
sunkaixuan 2017-06-04 10:12:53 +08:00
parent af50793708
commit 967e4cb23b
5 changed files with 47 additions and 7 deletions

View File

@ -0,0 +1,36 @@
using OrmTest.Demo;
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
/// <summary>
/// Secure string operations
/// </summary>
public class JoinSql : DemoBase
{
public static void Init()
{
var db = GetInstance();
//propertyName is valid
string propertyName = "Id";
string dbColumnName = db.EntityProvider.GetDbColumnName<Student>(propertyName);
var list = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
//propertyName is invalid
try
{
propertyName = "Id'";
dbColumnName = db.EntityProvider.GetDbColumnName<Student>(propertyName);
var list2 = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

View File

@ -39,6 +39,7 @@ namespace OrmTest
OrmTest.Demo.Delete.Init();
OrmTest.Demo.Update.Init();
OrmTest.Demo.DbFirst.Init();
OrmTest.Demo.JoinSql.Init();
}
}
}

View File

@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Demos\JoinSql.cs" />
<Compile Include="Demos\DbFirst.cs" />
<Compile Include="Demos\Delete.cs" />
<Compile Include="Demos\DemoBase.cs" />

View File

@ -57,7 +57,7 @@ namespace SqlSugar
var primaryField = primaryFields.Single();
foreach (var deleteObj in deleteObjs)
{
var entityPropertyName = this.Context.EntityProvider.GetEntityPropertyName<T>(primaryField);
var entityPropertyName = this.Context.EntityProvider.GetPropertyName<T>(primaryField);
var columnInfo = EntityInfo.Columns.Single(it => it.PropertyName == entityPropertyName);
var value = columnInfo.PropertyInfo.GetValue(deleteObj, null);
primaryKeyValues.Add(value);
@ -82,7 +82,7 @@ namespace SqlSugar
{
if (i == 0)
andString.Append(DeleteBuilder.WhereInAndTemplate + PubConst.Space);
var entityPropertyName = this.Context.EntityProvider.GetEntityPropertyName<T>(primaryField);
var entityPropertyName = this.Context.EntityProvider.GetPropertyName<T>(primaryField);
var columnInfo = EntityInfo.Columns.Single(it => it.PropertyName == entityPropertyName);
var entityValue = columnInfo.PropertyInfo.GetValue(deleteObj, null);
andString.AppendFormat(DeleteBuilder.WhereInEqualTemplate, primaryField, entityValue);

View File

@ -58,17 +58,19 @@ namespace SqlSugar
return mappingInfo == null ? tableName : mappingInfo.EntityName;
}
}
public string GetDbColumnName<T>(string entityPropertyName)
public string GetDbColumnName<T>(string propertyName)
{
var isAny=this.GetEntityInfo<T>().Columns.Any(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
Check.Exception(!isAny, "Property " + propertyName + " is Invalid");
var typeName = typeof(T).Name;
if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return entityPropertyName;
if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return propertyName;
else
{
var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.PropertyName == entityPropertyName);
return mappingInfo == null ? entityPropertyName : mappingInfo.DbColumnName;
var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.PropertyName == propertyName);
return mappingInfo == null ? propertyName : mappingInfo.DbColumnName;
}
}
public string GetEntityPropertyName<T>(string dbColumnName)
public string GetPropertyName<T>(string dbColumnName)
{
var typeName = typeof(T).Name;
if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return dbColumnName;