From 204ab9f7d8e07d82adff6a0de5f89d328b0d3394 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Wed, 20 Feb 2019 17:28:51 +0800 Subject: [PATCH] v0.1.0 & FreeSql.Repository --- .../Controllers/ValuesController.cs | 45 ------------------- .../Properties/launchSettings.json | 27 +++++++++++ FreeSql.Repository/BaseRepository.cs | 2 - FreeSql.Repository/FreeSql.Repository.csproj | 8 +++- FreeSql.Repository/IReadOnlyRepository.cs | 3 +- FreeSql.Tests/MySql/MySqlCodeFirstTest.cs | 3 ++ FreeSql.Tests/Oracle/OracleCodeFirstTest.cs | 3 ++ .../PostgreSQL/PostgreSQLCodeFirstTest.cs | 4 ++ .../SqlServer/SqlServerCodeFirstTest.cs | 3 ++ FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs | 3 ++ FreeSql/DataAnnotations/ColumnAttribute.cs | 6 ++- FreeSql/DataAnnotations/ColumnFluent.cs | 7 +++ FreeSql/FreeSql.csproj | 2 +- FreeSql/Internal/CommonUtils.cs | 1 + FreeSql/Internal/UtilsExpressionTree.cs | 14 +++++- 15 files changed, 78 insertions(+), 53 deletions(-) delete mode 100644 Examples/efcore_to_freesql/Controllers/ValuesController.cs create mode 100644 Examples/repository_01/Properties/launchSettings.json diff --git a/Examples/efcore_to_freesql/Controllers/ValuesController.cs b/Examples/efcore_to_freesql/Controllers/ValuesController.cs deleted file mode 100644 index e2ff226f..00000000 --- a/Examples/efcore_to_freesql/Controllers/ValuesController.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace efcore_to_freesql.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class ValuesController : ControllerBase - { - // GET api/values - [HttpGet] - public ActionResult> Get() - { - return new string[] { "value1", "value2" }; - } - - // GET api/values/5 - [HttpGet("{id}")] - public ActionResult Get(int id) - { - return "value"; - } - - // POST api/values - [HttpPost] - public void Post([FromBody] string value) - { - } - - // PUT api/values/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE api/values/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} diff --git a/Examples/repository_01/Properties/launchSettings.json b/Examples/repository_01/Properties/launchSettings.json new file mode 100644 index 00000000..c671858d --- /dev/null +++ b/Examples/repository_01/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:54379/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "repository_01": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:54383/" + } + } +} \ No newline at end of file diff --git a/FreeSql.Repository/BaseRepository.cs b/FreeSql.Repository/BaseRepository.cs index 2c2c9fd5..3edadbdc 100644 --- a/FreeSql.Repository/BaseRepository.cs +++ b/FreeSql.Repository/BaseRepository.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using System.Text; using System.Threading.Tasks; namespace FreeSql { diff --git a/FreeSql.Repository/FreeSql.Repository.csproj b/FreeSql.Repository/FreeSql.Repository.csproj index 97f3eb85..7b164deb 100644 --- a/FreeSql.Repository/FreeSql.Repository.csproj +++ b/FreeSql.Repository/FreeSql.Repository.csproj @@ -1,7 +1,13 @@ - + netstandard2.0 + 0.1.0 + YeXiangQin + 打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。 + https://github.com/2881099/FreeSql + FreeSql ORM Repository + true diff --git a/FreeSql.Repository/IReadOnlyRepository.cs b/FreeSql.Repository/IReadOnlyRepository.cs index bd26e7e8..ce3a9588 100644 --- a/FreeSql.Repository/IReadOnlyRepository.cs +++ b/FreeSql.Repository/IReadOnlyRepository.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace FreeSql { public interface IReadOnlyRepository : IRepository diff --git a/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs b/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs index 6c585839..74f0f388 100644 --- a/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs +++ b/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs @@ -25,6 +25,9 @@ namespace FreeSql.Tests.MySql { [Column(DbType = "varchar(200) not null", OldName = "title")] public string title222 { get; set; } = "10"; + + [Column(IsIgnore = true)] + public DateTime ct { get; set; } = DateTime.Now; } [Fact] diff --git a/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs b/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs index 099dc9f1..c75d472a 100644 --- a/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs +++ b/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs @@ -27,6 +27,9 @@ namespace FreeSql.Tests.Oracle { [Column(DbType = "varchar2(200 char) not null", OldName = "title")] public string title2 { get; set; } = "10"; + + [Column(IsIgnore = true)] + public DateTime ct { get; set; } = DateTime.Now; } [Fact] diff --git a/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs b/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs index 74720b16..b3f8f830 100644 --- a/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs +++ b/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs @@ -42,6 +42,10 @@ namespace FreeSql.Tests.PostgreSQL { //[Column(DbType = "varchar(100) not null", OldName = "title122333aaa")] //public string titleaaa { get; set; } = "fsdf"; + + + [Column(IsIgnore = true)] + public DateTime ct { get; set; } = DateTime.Now; } [Fact] diff --git a/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs b/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs index 44749891..5cc90bb9 100644 --- a/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs +++ b/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs @@ -31,6 +31,9 @@ namespace FreeSql.Tests.SqlServer { [Column(DbType = "varchar(100) not null", OldName = "title122333aaa")] public string titleaaa { get; set; } = "fsdf"; + + [Column(IsIgnore = true)] + public DateTime ct { get; set; } = DateTime.Now; } [Fact] diff --git a/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs b/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs index 0d519023..c38cac49 100644 --- a/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs +++ b/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs @@ -27,6 +27,9 @@ namespace FreeSql.Tests.Sqlite { [Column(DbType = "varchar(200) not null", OldName = "title2")] public string title3223 { get; set; } = "10"; + + [Column(IsIgnore = true)] + public DateTime ct { get; set; } = DateTime.Now; } [Fact] diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs index a730f424..dadde641 100644 --- a/FreeSql/DataAnnotations/ColumnAttribute.cs +++ b/FreeSql/DataAnnotations/ColumnAttribute.cs @@ -16,7 +16,7 @@ namespace FreeSql.DataAnnotations { /// public string DbType { get; set; } - internal bool? _IsPrimary, _IsIdentity, _IsNullable; + internal bool? _IsPrimary, _IsIdentity, _IsNullable, _IsIgnore; /// /// 主键 /// @@ -29,6 +29,10 @@ namespace FreeSql.DataAnnotations { /// 是否可DBNull /// public bool IsNullable { get => _IsNullable ?? false; set => _IsNullable = value; } + /// + /// 忽略此列,不迁移、不插入 + /// + public bool IsIgnore { get => _IsIgnore ?? false; set => _IsIgnore = value; } /// /// 数据库默认值 diff --git a/FreeSql/DataAnnotations/ColumnFluent.cs b/FreeSql/DataAnnotations/ColumnFluent.cs index a5e63721..4d05e5e1 100644 --- a/FreeSql/DataAnnotations/ColumnFluent.cs +++ b/FreeSql/DataAnnotations/ColumnFluent.cs @@ -50,5 +50,12 @@ namespace FreeSql.DataAnnotations { _column.IsNullable = value; return this; } + /// + /// 忽略此列,不迁移、不插入 + /// + public ColumnFluent IsIgnore(bool value) { + _column.IsIgnore = value; + return this; + } } } diff --git a/FreeSql/FreeSql.csproj b/FreeSql/FreeSql.csproj index 1ec86a51..1b554815 100644 --- a/FreeSql/FreeSql.csproj +++ b/FreeSql/FreeSql.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.0.14 + 0.1.0 true YeXiangQin 打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。 diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index e3e326b8..8d48cebe 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -72,6 +72,7 @@ namespace FreeSql.Internal { if (attr._IsPrimary == null) attr._IsPrimary = trycol.IsPrimary; if (attr._IsIdentity == null) attr._IsIdentity = trycol.IsIdentity; if (attr._IsNullable == null) attr._IsNullable = trycol.IsNullable; + if (attr._IsIgnore == null) attr._IsIgnore = trycol.IsIgnore; if (attr.DbDefautValue == null) attr.DbDefautValue = trycol.DbDefautValue; return attr; } diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index a0cd0afc..f81eb121 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -63,7 +63,9 @@ namespace FreeSql.Internal { IsIdentity = false, IsNullable = tp.Value.isnullable ?? true, IsPrimary = false, + IsIgnore = false }; + if (colattr.IsIgnore) continue; if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp?.dbtypeFull ?? "varchar(255)"; colattr.DbType = colattr.DbType.ToUpper(); @@ -101,7 +103,17 @@ 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 => a.Attribute.IsIdentity == true).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(); + if (trytb.Primarys.Any() == false) { + 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 }; + } + } + } foreach (var col in trytb.Primarys) col.Attribute.IsPrimary = true; }