Mapping EF Table(Name='')

This commit is contained in:
sunkaixuan
2018-11-10 15:07:49 +08:00
parent 8100454b81
commit 71382f8593
4 changed files with 29 additions and 4 deletions

View File

@@ -1,11 +1,16 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
/// <summary>
/// mapping ef attribute
/// </summary>
public class ExtEntity: DemoBase
{
public static void Init()
@@ -16,22 +21,36 @@ namespace OrmTest.Demo
IsAutoCloseConnection = true,
ConfigureExternalServices=new ConfigureExternalServices() {
EntityService = (property, column) => {
if (property.Name == "xxx") {
if (property.Name == "xxx") {// by name ignore column
column.IsIgnore = true;
}
//property.GetCustomAttributes
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
{
column.IsPrimarykey = true;
}
},
EntityNameService = (type,entity) => {
var attributes = type.GetCustomAttributes(true);
if (attributes.Any(it => it is TableAttribute))
{
entity.DbTableName = (attributes.First(it => it is TableAttribute)as TableAttribute).Name;
}
}
}
});
var sql=db.Queryable<StudentTest>().ToSql();
var sql2 = db.Insertable<StudentTest>(new StudentTest()).ToSql();
var sql=db.Queryable<StudentTest>().ToList();
var sql2 = db.Insertable<StudentTest>(new StudentTest()).ExecuteCommand();
}
}
[Table(Name ="student")]//default
public class StudentTest {
[Key]
public string Id { get; set; }
public string xxx { get; set; }
public string Name { get; set; }

View File

@@ -39,7 +39,9 @@
<HintPath>OtherDll\SyntacticSugar.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View File

@@ -28,6 +28,9 @@ namespace SqlSugar
var sugarTable = (SugarTable)sugarAttributeInfo;
result.DbTableName = sugarTable.TableName;
}
if (this.Context.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null) {
this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService(type,result);
}
result.Type = type;
result.EntityName = result.Type.Name;
result.Columns = new List<EntityColumnInfo>();

View File

@@ -94,5 +94,6 @@ namespace SqlSugar
public Action<PropertyInfo, EntityColumnInfo> EntityService{ get; set; }
public Action<Type,EntityInfo> EntityNameService { get; set; }
}
}