Coder Social home page Coder Social logo

shuxinqin / chloe Goto Github PK

View Code? Open in Web Editor NEW
1.5K 1.5K 456.0 19.54 MB

A lightweight and high-performance Object/Relational Mapping(ORM) library for .NET --C#

Home Page: https://github.com/shuxinqin/Chloe/wiki

License: MIT License

C# 99.90% Batchfile 0.10%
c-sharp chloe dameng dotnet dotnetcore kingbasees mysql oracle orm postgresql sqlite

chloe's People

Contributors

netcore-jroger avatar shuxinqin avatar softworm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chloe's Issues

不建议使用双if的方式锁定资源

我在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的时候主键不赋值就会报错,有没有办法解决呢

hi 为什么mysql 会报错

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

The host does not support SSL connections.

Mysql The host does not support SSL connections.
server=localhost;port=3306;database=member;user=root;password=123456;SslMode=none;
我已经加了SslMode=none了还是无效;

关于主键默认自增的建议

先申明一下,并不是挑刺.
看到了你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这些带下划线的字段,没有值,不知道哪儿里错了?

postgresql查询

大佬,postgres实体转查询语句的时候,全都小写了。如果数据库字段大小写区分的话(postgres数据库区分大小写),添加特性也没有用,依旧是小写的

使用sqllite3 tolist报错请问是怎么回事呢?

在 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. ---&gt; 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});

NuGet安装的Chloe.SqlServer,没有DoWithTransaction

我是参照ace网站里面的写法在那自己学习,那里面用DoWithTransaction来操作数据库同时记日志,我没看错的话,但是我自己用的时候DbContext里面没有DoWithTransaction这个方法,ace里面的DoWithTransaction也确实没法转到实现,这是我哪里姿势不对么?

SUM() 当记录为空时会抛出异常

假设存在如下实体:
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。

Idea: Impliment System.Linq.IQueryProvider

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?

Select条件在mysql的情况下,怎么也出不来

    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都实验过了,

DotNetCore一些优化建议

粗略试用了一下,发现还是有些问题,可能是我用的方法不对。
1.优化 增删查改相关参数值参数化,这个很重要,如果没有参数化,程序很容易受SQL注入式攻击
2.新增 打印SQL和参数
3.新增 实体类字段加忽略标识,查表或增改忽略该字段
4.优化 UPDATE时支持LINQ格式: context.Update < Entity > (new { name="xxx",title="xxxx" },p=>p.id==1);

没有必要锁定的资源

在 Chloe/Chloe/Descriptors/PropertyDescriptor.cs 中
资源:this._valueGetter,无论哪个线程创建他,其结果都是相等的,所以我建议无需锁定,而是直接:
if (this._valueGetter == null){
this._valueGetter = Get....
}

return this._valueGetter;

枚举类型为 byte 类型时,将枚举值转换为数据库值报错

能否增加数据库类型 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 项目?

现在公司在做 UWP 项目,需要用到 ORM 框架,用了一段时间微软推荐的 EF 框架,效果不理想,准备更换框架。
请问该 Chloe 是否可以用于 UWP 项目?

在迁移.netcore遇到的问题

在迁移.netcore的时候怎么怎么替换下边这段代码.我想动态的创建conn,这个只要更改配置就可以修改使用的数据库.

public IDbConnection CreateConnection()
        {
            IDbConnection conn = DbProviderFactories.GetFactory(_config.ProviderName).CreateConnection();
            conn.ConnectionString = _config.ConnectionStr;
            return conn;
        }

所以向您请教一下.应该怎么做.在这里先感谢您.

关于 2.22.0 版本的建议

个人见解,程序本身应该尽可能的向前兼容。

有关 Lambda 中考虑 null 值的问题,应该算做一个 Feature 而非 BugFix,那么不应该是简单粗暴的做破坏性升级(即新旧版本表现不一致)。

往往当使用 a.XName = b.YName 来做联结查询条件的情况,XName 和 YName 在 a、b 中一般都会有索引(实际充当了逻辑外键),且很少需要考虑 IS NULL 的情况,因为往往两边都是非空;而更重要的是,IS NULL 这种情况,是不会命中索引的!

所以我更建议建议把 Sql.Equals 和 Sql.NotEquals 当作该版本新增的 Feature 的实现方法,而不是改变已有的 Lambda ==,这样对以前的代码反而造成了影响。

一家之言,欢迎探讨。

sqlite dbtransaciton

当使用Sqlite驱动时候,发现DbContext.Session.CurrentTransaction返回的事务不是DbTransaction类型的,而且DbContext.Session.CurrentTransaction返回的事务类型是internal类型,外部无法使用。希望能将底层事务类型调整为System.Data通用事务类型,或者最起码,在接口/API层面公布底层Transaction。否则没法和外部SQL操作纳入同一个事务。

Oracle In 1000

没有考虑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等.

连接查询select如何返回指定类型

实体类 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群满了,什么时候考虑开个新群啊

where 条件的问题

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

关于Context上下文回收问题

Chole文档上说,注意:DbContext 实例非线程安全,不能设置为单例。同时,用完务必要将 DbContext 释放。这里指的是使用using显式释放资源吗?因为项目一般是分为多层,所以可能因为代码原因导致在一次请求中使用了不同的上下文,也就是您所说的DbContext 实例非线程安全。因为会使用unitwork模式来实现事务操作,所以维护上下文也是一项很重要的工作。目前我从网上搜索到的方案一般是:
1.保证线程级别的安全 例如使用HttpContext
2.仓储采用依赖注入模式,并且使用ioc框架,利用其进行生命周期的管理
如果使用这两种方案是不是就意味着不用进行using显式释放了呢?

希望您能帮忙解惑一下,感谢!最后给Chloe点赞,非常喜欢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.