Comments (32)
意见收到,现在过年期间,年后向您请教
from freesql.
有新的进展了吗?
from freesql.
不好意思,差点忘记了
from freesql.
using FreeSql.DataAnnotations;
using Xunit;
namespace FreeSql.Tests.DataAnnotations {
public class g {
public static IFreeSql mysql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.UseAutoSyncStructure(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象,在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象,在执行后
.UseLazyLoading(true)
.Build();
}
public class FluentTest {
[Fact]
public void Fluent() {
g.mysql.CodeFirst
.ConfigEntity<TestFluenttb1>(a => {
a.Name("xxdkdkdk1").SelectFilter("a.Id22 > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
})
.ConfigEntity<TestFluenttb2>(a => {
a.Name("xxdkdkdk2").SelectFilter("a.Idx > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
});
var ddl1 = g.mysql.CodeFirst.GetComparisonDDLStatements<TestFluenttb1>();
var ddl2 = g.mysql.CodeFirst.GetComparisonDDLStatements<TestFluenttb2>();
var t1id = g.mysql.Insert<TestFluenttb1>().AppendData(new TestFluenttb1 { }).ExecuteIdentity();
var t1 = g.mysql.Select<TestFluenttb1>(t1id).ToOne();
var t2lastId = g.mysql.Select<TestFluenttb2>().Max(a => a.Id);
var t2affrows = g.mysql.Insert<TestFluenttb2>().AppendData(new TestFluenttb2 { Id = t2lastId + 1 }).ExecuteAffrows();
var t2 = g.mysql.Select<TestFluenttb2>(t2lastId + 1).ToOne();
}
}
class TestFluenttb1 {
public int Id { get; set; }
public string name { get; set; } = "defaultValue";
}
[Table(Name = "cccccdddwww")]
class TestFluenttb2 {
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
public int Id { get; set; }
public string name { get; set; } = "defaultValue";
}
}
这是测试代码,目前已经发布了
from freesql.
`///
/// 映射表
///
protected override void MapTable( EntityTypeBuilder builder ) {
builder.ToTable( "auth" );
}
/// <summary>
/// 映射属性
/// </summary>
protected override void MapProperties( EntityTypeBuilder<Auth> builder ) {
//Id编号
builder.Property(t => t.Id)
.HasColumnName("id");
}`
from freesql.
目前设定方法名和Attribute是一样的,看上去比较尬,哈哈
from freesql.
有办法改进支持一下吗?我想将你的这个框架和我们现在的系统结合起来用
from freesql.
具体你可以看一下Util这个开源项目
from freesql.
正好昨天在看util
是兼容这一段吗?
from freesql.
是的,因为目前实体没法共用,会提示找不到字段
from freesql.
可否方便发一些实体+map demo 作参考
from freesql.
from freesql.
我们这主要也就是主键Id字段的名字会不一样。你可以直接在Util的Demo基础上实现功能就能满足需求了。
from freesql.
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace FreeSql.Extensions.EFCoreModelBuilder {
public static class CodeFirstExtensions {
public static void ConfigEntity(this ICodeFirst codeFirst, ModelBuilder modelBuilder) {
foreach (var type in modelBuilder.Model.GetEntityTypes()) {
codeFirst.ConfigEntity(type.ClrType, a => {
//表名
var relationalTableName = type.FindAnnotation("Relational:TableName");
if (relationalTableName != null) {
a.Name(relationalTableName.Value?.ToString() ?? type.ClrType.Name);
}
foreach (var prop in type.GetProperties()) {
var freeProp = a.Property(prop.Name);
//列名
var relationalColumnName = prop.FindAnnotation("Relational:ColumnName");
if (relationalColumnName != null) {
freeProp.Name(relationalColumnName.Value?.ToString() ?? prop.Name);
}
//主键
freeProp.IsPrimary(prop.IsPrimaryKey());
//自增
freeProp.IsIdentity(prop.GetAnnotations().Where(z =>
z.Name == "SqlServer:ValueGenerationStrategy" && z.Value.ToString().Contains("IdentityColumn") //sqlserver 自增
).Any());
//可空
freeProp.IsNullable(prop.AfterSaveBehavior != Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Throw);
//类型
var relationalColumnType = prop.FindAnnotation("Relational:ColumnType");
if (relationalColumnType != null) {
var dbType = relationalColumnType.ToString();
if (!string.IsNullOrEmpty(dbType)) {
var maxLength = prop.FindAnnotation("MaxLength");
if (maxLength != null)
dbType += $"({maxLength})";
freeProp.DbType(dbType);
}
}
}
});
}
}
}
}
你先看看这段代码。使用的是扩展方法,意图将 EF ModelBuilder 对象直接配置为 FreeSql 的实体。
是否可行?
更新日志:
https://github.com/2881099/FreeSql/wiki/%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97
代码:
FreeSql.Extensions.EFCoreModelBuilder/CodeFirstExtensions.cs
from freesql.
https://www.cnblogs.com/kellynic/p/10384165.html
在 EFCore 定义的实体中进行 FreeSql 开发
from freesql.
我看看
from freesql.
还需要针对每个实体来做下面这种配置吗?
fsql.CodeFirst
.ConfigEntity(a => {
a.Name("xxdkdkdk1").SelectFilter("a.Id22 > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
})
.ConfigEntity(a => {
a.Name("xxdkdkdk2").SelectFilter("a.Idx > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
});
from freesql.
不需要啊,在Ef fuentapi 或onmodelcreating 配置
from freesql.
有没有具体的Demo?
from freesql.
v0.0.14
- ICodeFirst.ConfigEntity 可更新实体配置已缓存的数据;
- 防止同连接字符串被IFreeSql使用多次,发生连接池溢出bug(ado.net连接池原理,减少解释成本);
- 增加 efcore_to_freesql 示例项目,现实与 EFCore 实体共存;
示例请见源码:
https://github.com/2881099/FreeSql/tree/master/Examples/efcore_to_freesql
在 Startup.cs 测试的,请看注释执行的SQL语句。如果EF实体配置过映射,在执行任意EF操作后将配置同步至FreeSql中。
from freesql.
所有 EFCore DBContext 继承 BaseDBContext
public class BaseDBContext : DbContext {
public static IFreeSql Fsql { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
Fsql.CodeFirst.ConfigEntity(modelBuilder.Model); //同步配置
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseSqlite(@"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10");
}
}
from freesql.
和Util的一起使用有冲突,会导致原本的不可用。
from freesql.
方便发个冲突小demo我的邮箱吗,[email protected]
from freesql.
晚点我发个Demo给你看一下。
from freesql.
你可以试一下那些表名或者字段名带下划线的,一起使用的时候会导致映射的表名下拉线没有了,感觉好像没有起作用一样的。
from freesql.
测试了,下划线不会去掉啊。建议你还是发个demo给我
from freesql.
这样直接用肯定没问题,你结合一下util使用试试。
from freesql.
整个可运行的demo呀,我不知道util的使用习惯。
from freesql.
我看了一下你的Demo,需要在Startup里配置一次数据库链接,也还需要在BaseDBContext里配置一次链接吗?
from freesql.
只要配置一次就行,要么starup,或者basedbcontext那里
from freesql.
看一下QQ
from freesql.
能不能弄个单独只是基于basedbcontext的demo,我看了一下你的案例更多的是基于startup的
from freesql.
Related Issues (20)
- 升级衍生bug,原功能 "Api 命名换成了 SetDtoIgnore,使用习惯与 SetSourceIgnore 一致" HOT 7
- 实体的属性的 Column 定义了 sql 返回中不存在的字段 报错 HOT 7
- 参数化异常 UseGenerateCommandParameterWithLambda(true) HOT 3
- 在Select中使用子查询Any 生成SQL有语法错误 HOT 2
- Sqlite 数据库子查询枚举转换问题 HOT 2
- 查询时枚举转 int再转字符串报错 referenced from scope '', but it is not defined HOT 1
- WithTempQuery HOT 1
- mysql连接数据库字符串如果密码里面带有分号则属于特殊字符,导致无法连接 HOT 1
- InsertOrUpdateDict中null值插入成空字符串'' HOT 8
- CreateInstance 导致可为空的DataTime 赋值为0001-01-01 时间问题 HOT 3
- SqlServer表列类型为TimeSpan,Update出现异常 HOT 2
- MySql 8.0.28 Emoji 字符 插入错误 HOT 3
- Where 方法 使用like 的奇怪现象 HOT 1
- 级联查询,能否提供一个只包含某些字段的功能
- mysql 的timespan 類型,表結構是生成time類型,但是執行select時,生成的sql好像有問題 HOT 3
- 业务高峰时段数据库操作报异常System.AggregateException: One or more errors occurred. (Name or service not known) HOT 3
- FreeSql Unity使用问题 HOT 4
- DBFirst实体生成,oracle number(1)会映射为bool类型 HOT 1
- pgsql 使用freesql生成器生成数组类型错误 HOT 1
- IgnoreColumns不能忽略On Conflict Do Update的自增列吗? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freesql.