shuxinqin / chloe Goto Github PK
View Code? Open in Web Editor NEWA lightweight and high-performance Object/Relational Mapping(ORM) library for .NET --C#
Home Page: https://github.com/shuxinqin/Chloe/wiki
License: MIT License
A lightweight and high-performance Object/Relational Mapping(ORM) library for .NET --C#
Home Page: https://github.com/shuxinqin/Chloe/wiki
License: MIT License
我在Chloe/Chloe/Core/Emit/ClassGenerator.cs,下看见你用if lock if的方式控制并发资源的锁定,建议使用并发安全的字典:ConcurrentDictionary。他在.net 4之后就已经提供。
如果你要支持.net 4之前的版本,使用读写锁也比这种方式性能好,况且这种方式实际是不安全的,因为在if访问时,使用了可能变动的字段。
以下是本人的一点小小建议,如若有理希望伟大的开发者采纳
Where可否支持如下
IQuery<User> q = context.Query<User>();
Where<User> where = new Where<User>();
where.And(a=>a.name == "zhangsan");
where.Or(a=>a.name == "lisi");
var users = q. Where(where).ToList();
另外比如数据库的主键Guid类型的,插入的时候是数据库默认的,但是在代码里面Insert的时候主键不赋值就会报错,有没有办法解决呢
不支持多列联合主键?
Does not support method 'Int32.ToString()'
Where(a => a.Id.ToString().Contains("ddd"))
实体有部分属性是继承自父类的,在查询的时候会报错。
\Chloe\Query\MappingObjectExpression.cs
GetMemberExpression方法,返回了NULL值。
System.Data.SqlClient.SqlException:“A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)”
Win32Exception: 找不到网络路径。
我用原生 读取mysql没报错 可以读取数据 用 chloe 报错 能回复一下吗 谢谢 @shuxinqin
我是mssql迁移mysql mssql也是Chloe
Mysql The host does not support SSL connections.
server=localhost;port=3306;database=member;user=root;password=123456;SslMode=none;
我已经加了SslMode=none了还是无效;
oracle not support like ? escape ?
is there anyway to to do batch insert ?
先申明一下,并不是挑刺.
看到了你TypeDescriptor.cs里101行的说明.如果实体只存在1个主键,属性为支持自增的数据类型,并且没有显式为属性标记非自增特性,则默认属性映射为自增列.这点我觉得不太合理,甚至设计自增和非自增两个Attribute的存在也不合理.
1,如果我数据库主键就是int型,非自增为主键.该主键由业务计算,由代码校验确认全局唯一性,或者如snowflake这类算法生成全局唯一ID.并非自增,如何支持?
2,对于自增标识特性,从逻辑上理解,标识了就是,未标识就不是.设计自增与非自增两个映射特性,有点冗余.(未了解Oracle,MySql等是否同时存在自增与非自增两种列设置功能)
CREATE TABLE sms_record
(
Send_Time
DATETIME NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发送时间',
Create_Time
DATETIME NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
Sms_Type
TINYINT(4) NULL DEFAULT '1' COMMENT '1 消费 2充值',
User_id
INT(11) NULL DEFAULT '0',
Store_id
INT(11) NULL DEFAULT '0',
Send_Id
VARCHAR(33) NULL DEFAULT NULL,
PRIMARY KEY (Id
)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;
[Table("sms_record")] public class SmsRecord { public SmsRecord() { } [Column("Send_Time")] public DateTime SendTime { get; set; } [Column("Create_Time")] public DateTime CreateTime { get; set; } [Column("Sms_Type")] public int SmsType { get; set; } [Column("User_id")] public int UserId { get; set; } [Column("Store_id")] public int StoreId { get; set; } }
public List GetUnSend(int pageSize)
{
var sql = "select * from sms_record where State=0 order by Id asc limit 0," + pageSize;
return this._context.SqlQuery(sql).ToList();
}
Send_Time, Create_Time这些带下划线的字段,没有值,不知道哪儿里错了?
大佬,postgres实体转查询语句的时候,全都小写了。如果数据库字段大小写区分的话(postgres数据库区分大小写),添加特性也没有用,依旧是小写的
asp.net core的项目 用注入的方式 怎么可以连接多个库
manager.Insert(brandsinfo);
我的ID是人为设置的,不是自增列,怎么写?
#请问是否有考虑加入 Task 方法调用的支持。
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()Chloe.Exceptions.ChloeException: An error occurred while mapping the column 'Id'(0,integer,System.Int64). For details please see the inner exception. ---> System.TypeAccessException: 方法“Chloe.Mapper.MRMs.ImageInfo_Id_ded071.Map(System.Object, System.Data.IDataReader, Int32)”尝试访问类型“PictureDetective.Modle.ImageInfo”失败。
在 Chloe.Mapper.MRMs.ImageInfo_Id_ded071.Map(Object , IDataReader , Int32 )
在 Chloe.Mapper.ObjectActivator.CreateInstance(IDataReader reader)
数据库中id是integer类型
更新数据的时候,目前的方式写的代码稍微有点繁琐,
能否增加匿名方法的支持,像下面这样:
public class TestModel { public int Id{get;set;} public string Name{get;set;} }
var m=new TestModel();
m.Name="TestName";
现在的更新方式:
db.Update(p=>p.Id==1,a=>new TestModel{Name=m.Name});
期望支持的方式:
db.Update(p=>p.Id==1,new {m.Name});
我是参照ace网站里面的写法在那自己学习,那里面用DoWithTransaction来操作数据库同时记日志,我没看错的话,但是我自己用的时候DbContext里面没有DoWithTransaction这个方法,ace里面的DoWithTransaction也确实没法转到实现,这是我哪里姿势不对么?
假设存在如下实体:
class Demo { public int Id {get; set;} public int Amount {get; set;} }
那么执行:
int ct = dbContext.Query<Demo>().Sum(d => d.Amount);
如果 Demo 表中不存在记录,或是加上 Where 条件后记录为空,那么会抛出类型转换异常。(同理的还有 AVG())
而如果将 Amount 字段声明为 int? 类型就不会抛异常。但首先这个字段在数据库中本身就是 NOT NULL 的,在代码中也应该强约束为 int 而非 int?。
建议在 SqlGenerator_Helper.cs 下的 AppendAggregateFunction() 方法里做如下修改:
/原始代码省略/
if (withCast)
{
generator._sqlBuilder.Append("COALESCE("); // 此处添加
/原始代码省略/
}
/原始代码省略/
if (withCast)
{
generator._sqlBuilder.Append(", 0)"); // 此处添加
}
这样当 SUM() 结果为 NULL 时会自动转换成 0。
I was looking for an orm that could take a System.Data.IDbConnection
, and create System.Linq.IQueryable
objects. This lib comes so close!
It's for users of the library I'm working on which accept the linq IQueryable.
Any plans to add a IQueryable wrapper that maps to your LINQ-like IQuery interface?
public IEnumerable<TResult> GetListWithoutConn<TResult>(int brandsid)
{
return manager.Query<BrandsConnEntity>().Select(p => new { p.ID, p.BrandsID, p.DataBaseName, p.ConnectionType }).Where(p => p.BrandsID == brandsid).ToList<TResult>();
}
不管是用官网的context.Query().Ignore("Name", "Age").ToList();还是用Select都实验过了,
遇到单表自身连接查询咋弄?
粗略试用了一下,发现还是有些问题,可能是我用的方法不对。
1.优化 增删查改相关参数值参数化,这个很重要,如果没有参数化,程序很容易受SQL注入式攻击
2.新增 打印SQL和参数
3.新增 实体类字段加忽略标识,查表或增改忽略该字段
4.优化 UPDATE时支持LINQ格式: context.Update < Entity > (new { name="xxx",title="xxxx" },p=>p.id==1);
比如:
模型->表名: UserGroup->user_group
属性->字段: UserId->user_id
做成选配项,不用每次都标注特性
比如数据库 是 string,用 object 接收就会报错。
另外数据库 是int,但是数据都是 0/1,用 bool 接收也会报错。
RT
数据表里的主键都是无符号的,报"Auto increment member type must be Int16, Int32 or Int64."
是否考虑dotnet core的支持
在 Chloe/Chloe/Descriptors/PropertyDescriptor.cs 中
资源:this._valueGetter,无论哪个线程创建他,其结果都是相等的,所以我建议无需锁定,而是直接:
if (this._valueGetter == null){
this._valueGetter = Get....
}
return this._valueGetter;
能否增加数据库类型 tinyint 支持?
Chloe.Exceptions.ChloeException: Please make sure that the member of the column 'Foo'(7,tinyint,System.Byte) map is the correct type. ---> System.InvalidCastException: 指定的转换无效。
在 System.Data.SqlClient.SqlBuffer.get_Int32()
在 System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
在 Chloe.Core.InternalDataReader.GetInt32(Int32 i)
在 Chloe.Extensions.DataReaderExtension.GetEnum[TEnum](IDataReader reader, Int32 ordinal)
在 Chloe.Mapper.MRMs.ProductPlanEnterEntity_State_a3fb841.Map(Object , IDataReader , Int32 )
在 Chloe.Mapper.MappingMemberBinder.SetValue(Object obj, IDataReader reader)
在 Chloe.Mapper.ObjectActivator.CreateInstance(IDataReader reader)
实体插入数据,返回的是实体本身,呢怎么判断插入失败的呢
现在公司在做 UWP 项目,需要用到 ORM 框架,用了一段时间微软推荐的 EF 框架,效果不理想,准备更换框架。
请问该 Chloe 是否可以用于 UWP 项目?
在迁移.netcore的时候怎么怎么替换下边这段代码.我想动态的创建conn,这个只要更改配置就可以修改使用的数据库.
public IDbConnection CreateConnection()
{
IDbConnection conn = DbProviderFactories.GetFactory(_config.ProviderName).CreateConnection();
conn.ConnectionString = _config.ConnectionStr;
return conn;
}
所以向您请教一下.应该怎么做.在这里先感谢您.
个人见解,程序本身应该尽可能的向前兼容。
有关 Lambda 中考虑 null 值的问题,应该算做一个 Feature 而非 BugFix,那么不应该是简单粗暴的做破坏性升级(即新旧版本表现不一致)。
往往当使用 a.XName = b.YName 来做联结查询条件的情况,XName 和 YName 在 a、b 中一般都会有索引(实际充当了逻辑外键),且很少需要考虑 IS NULL 的情况,因为往往两边都是非空;而更重要的是,IS NULL 这种情况,是不会命中索引的!
所以我更建议建议把 Sql.Equals 和 Sql.NotEquals 当作该版本新增的 Feature 的实现方法,而不是改变已有的 Lambda ==,这样对以前的代码反而造成了影响。
一家之言,欢迎探讨。
字符串长度小于4000的时候可以进库,但是大于4000的时候就会报错了。请问这个怎么映射?
当使用Sqlite驱动时候,发现DbContext.Session.CurrentTransaction返回的事务不是DbTransaction类型的,而且DbContext.Session.CurrentTransaction返回的事务类型是internal类型,外部无法使用。希望能将底层事务类型调整为System.Data通用事务类型,或者最起码,在接口/API层面公布底层Transaction。否则没法和外部SQL操作纳入同一个事务。
没有考虑Oracle in 语句最多不能超多1000个.
var str = qq1.Select(p => p.FID).ToArray(); //2000个字符串数组
var qq = Ctx.Query<T_MATERIAL>().Where (p=>str.Contains (p.FID ));
这个有异常.
另外是否可以考虑支持 客户端的表达式创建和序列化问题 ,如 JSON等.
比如拼音字母查询:Where(x=>x.Content.ToPY().Contians("OK"))
实体类 InvClass 主键 InvCCode;Inventory 主键 InvCode 外键 InvCCode,
Inventory 有类型为 InvClass 的属性。
定义Inventory 左连接 InvClass查询
var joiningQuery = context.Query().LeftJoin((inventory, invclass) => inventory.InvCCode == class.InvCCode);
连接查询对象Select得到匿名类型
var query = joiningQuery.Select((inventory, invclass) => new {Inventory = inventory, invClass = invclass});
Select 返回指定类型
var result = query.Select(a=>new Inventory{ InvCode = a.Inventory.InvCode, ... ... InvClass = a.invClass});
此处,返回指定类型时,能不能返回Inventory,并且对 Inventory.InvClass 进行赋值, 不需要对其中大量的属性在新建对象中一个一个赋值
另,QQ群满了,什么时候考虑开个新群啊
Expression<Func<Base_Brand, bool>> we = x => x.BrandCode =="123";
Expression<Func<Base_Brand, bool>> wea = x => x.BrandName == "2";
we = we.And<Base_Brand>(wea);
public static Expression<Func<T, bool>> And(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, expr2.Body), expr1.Parameters);
}
表达式拼凑就报错。
Expression<Func<Base_Brand, bool>> we = x => x.BrandCode =="123"&&x.BrandName=="23";
这样就没有问题。这2个有什么区别吗。不都是表达的拼凑吗
1、API设计的非常优雅。很喜欢,
2、在调用储存过程中,希望可以借鉴下Dapper的模式,比如大多数的时候可能都喜欢这样使用。
var dbParm = new DbParam();
dbParm.AddDynamic(new {Name="",Sex=0}); //都是一样的输入类型参数,可能会选择匿名类 或者实体类
dbParm.Add("OutMsg","",DbType.String,ParamDirection.Output);
dbParm.Add("OutMsg1","",DbType.String,ParamDirection.Output);
//执行方法
ontext.Session.ExecuteNonQuery("PROC_NAME",dbParm);
//获取的想要的输出参数
dbParm.Get<string>("OutMsg");
dbParm.Get<string>("OutMsg1");
还有我的储存过程 可能会返回数据集,或许我并不知道它的多少。可能是DataTable 也可能是DataSet类型。或者我会返回输出参数+数据集一起都出现。
3、希望能把所有的库跟核心库融合在一起,因为并不想去选择对应版本的类库。希望这件事由代码帮我去做.
希望层主加油,非常感谢你的分享库,
如题
如果能把多数据库支持集中到单个dll,使用更轻巧方便。可以参考FluentData
Chole文档上说,注意:DbContext 实例非线程安全,不能设置为单例。同时,用完务必要将 DbContext 释放。这里指的是使用using显式释放资源吗?因为项目一般是分为多层,所以可能因为代码原因导致在一次请求中使用了不同的上下文,也就是您所说的DbContext 实例非线程安全。因为会使用unitwork模式来实现事务操作,所以维护上下文也是一项很重要的工作。目前我从网上搜索到的方案一般是:
1.保证线程级别的安全 例如使用HttpContext
2.仓储采用依赖注入模式,并且使用ioc框架,利用其进行生命周期的管理
如果使用这两种方案是不是就意味着不用进行using显式释放了呢?
希望您能帮忙解惑一下,感谢!最后给Chloe点赞,非常喜欢!
类似EF 模型检测初始化,改变自动生成sql之类的东西。
你好 目前还不支持批量插入数据吗?
如果我有一千条,如何批量更新?
比如和ef的扩展DynamicFilters
按字段分组查询最新的数据该怎么用方法写呢?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.