You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.0 KiB
58 lines
2.0 KiB
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace OrmTest.Demo
|
|
{
|
|
/// <summary>
|
|
/// mapping ef attribute
|
|
/// </summary>
|
|
public class ExtEntity: DemoBase
|
|
{
|
|
public static void Init()
|
|
{
|
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {
|
|
ConnectionString = Config.ConnectionString,
|
|
DbType = DbType.PostgreSQL,
|
|
IsAutoCloseConnection = true,
|
|
ConfigureExternalServices=new ConfigureExternalServices() {
|
|
EntityService = (property, column) => {
|
|
if (property.Name == "xxx") {// by name ignore column
|
|
column.IsIgnore = true;
|
|
}
|
|
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>().ToList();
|
|
var sql2 = db.Insertable<StudentTest>(new StudentTest()).ExecuteCommand();
|
|
}
|
|
|
|
}
|
|
|
|
[Table("student")]//default
|
|
public class StudentTest {
|
|
|
|
[Key]
|
|
public string Id { get; set; }
|
|
public string xxx { get; set; }
|
|
public string Name { get; set; }
|
|
}
|
|
}
|
|
|