- 优化插入判断主键,且为 Guid/Guid? 类型,并且值为 null/Guid.Empty 时,将插入的值变为 FreeUtil.NewMongodbId()

This commit is contained in:
28810 2019-03-04 20:45:19 +08:00
parent 24987ad2e9
commit a189b6abba
6 changed files with 24 additions and 15 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.7</Version>
<Version>0.1.9</Version>
<Authors>YeXiangQin</Authors>
<Description>FreeSql 通用仓库层实现,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>

View File

@ -35,9 +35,7 @@ namespace FreeSql.Tests.Sqlite {
var commentRepository = g.sqlite.GetGuidRepository<Comment>();
//添加测试文章
var topicId = FreeUtil.NewMongodbId();
topicRepository.Insert(new Topic {
Id = FreeUtil.NewMongodbId(),
var topic = topicRepository.Insert(new Topic {
Title = "文章标题1",
Content = "文章内容1",
CreateTime = DateTime.Now
@ -45,12 +43,11 @@ namespace FreeSql.Tests.Sqlite {
//添加10条测试评论
var comments = Enumerable.Range(0, 10).Select(a => new Comment {
Id = FreeUtil.NewMongodbId(),
TopicId = topicId,
TopicId = topic.Id,
Nickname = $"昵称{a}",
Content = $"评论内容{a}",
CreateTime = DateTime.Now
});
}).ToArray();
var affrows = commentRepository.Insert(comments);
var find = commentRepository.Select.Where(a => a.Topic.Title == "文章标题1").ToList();

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.7</Version>
<Version>0.1.9</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>打造 .NETCore 最方便的 ORMDbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>

View File

@ -80,7 +80,13 @@ namespace FreeSql.Internal.CommonProvider {
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
if (colidx2 > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteWriteParamter(col.CsType, $"{_commonUtils.QuoteParamterName(col.CsName)}{didx}"));
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : null);
object val = null;
if (_table.Properties.TryGetValue(col.CsName, out var tryp)) {
val = tryp.GetValue(d);
if (col.Attribute.IsPrimary && (col.CsType == typeof(Guid) || col.CsType == typeof(Guid?))
&& (val == null || (Guid)val == Guid.Empty)) tryp.SetValue(d, val = FreeUtil.NewMongodbId());
}
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, val);
++colidx2;
}
sb.Append(")");

View File

@ -113,14 +113,14 @@ namespace FreeSql.Internal {
}
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, "id", true) == 0).ToArray();
var identcols = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).FirstOrDefault();
if (identcols != null) trytb.Primarys = new[] { identcols };
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, $"{trytb.DbName}id", true) == 0).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, "id", true) == 0).ToArray();
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, $"{trytb.DbName}_id", true) == 0).ToArray();
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, $"{trytb.DbName}id", true) == 0).ToArray();
if (trytb.Primarys.Any() == false) {
var identcols = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).FirstOrDefault();
if (identcols != null) trytb.Primarys = new[] { identcols };
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, $"{trytb.DbName}_id", true) == 0).ToArray();
}
}
}

View File

@ -52,7 +52,13 @@ namespace FreeSql.Oracle.Curd {
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
if (colidx2 > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteWriteParamter(col.CsType, $"{_commonUtils.QuoteParamterName(col.CsName)}{didx}"));
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : null);
object val = null;
if (_table.Properties.TryGetValue(col.CsName, out var tryp)) {
val = tryp.GetValue(d);
if (col.Attribute.IsPrimary && (col.CsType == typeof(Guid) || col.CsType == typeof(Guid?))
&& (val == null || (Guid)val == Guid.Empty)) tryp.SetValue(d, val = FreeUtil.NewMongodbId());
}
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, val);
++colidx2;
}
}