Skip to content

Commit 6cba1de

Browse files
committed
- 修复 DbFirst string not null 未生成特性 IsNullable = false 的问题;#678
1 parent d7cc7ce commit 6cba1de

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

Extensions/FreeSql.Generator/RazorModel.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@
99
using System.Text;
1010
using System.Text.RegularExpressions;
1111

12-
public class RazorModel {
13-
public RazorModel(IFreeSql fsql, string nameSpace, bool[] NameOptions, List<DbTableInfo> tables, DbTableInfo table) {
12+
public class RazorModel
13+
{
14+
public RazorModel(IFreeSql fsql, string nameSpace, bool[] NameOptions, List<DbTableInfo> tables, DbTableInfo table)
15+
{
1416
this.fsql = fsql;
15-
this.NameSpace = nameSpace;
16-
this.NameOptions = NameOptions;
17-
this.tables = tables;
17+
this.NameSpace = nameSpace;
18+
this.NameOptions = NameOptions;
19+
this.tables = tables;
1820
this.table = table;
1921
}
2022

2123
public IFreeSql fsql { get; set; }
2224
public bool[] NameOptions { get; set; }
23-
public List<DbTableInfo> tables { get; set; }
25+
public List<DbTableInfo> tables { get; set; }
2426
public DbTableInfo table { get; set; }
2527
public List<DbColumnInfo> columns => this.table.Columns;
2628
public string NameSpace { get; set; }
27-
public string FullTableName => $"{(new[] { "public", "dbo" }.Contains(table.Schema) ? "" : table.Schema)}.{table.Name}".TrimStart('.');
29+
public string FullTableName => $"{(new[] { "public", "dbo" }.Contains(table.Schema) ? "" : table.Schema)}.{table.Name}".TrimStart('.');
2830

29-
public string GetCsName(string name) {
31+
public string GetCsName(string name)
32+
{
3033
name = Regex.Replace(name.TrimStart('@', '.'), @"[^\w]", "_");
3134
name = char.IsLetter(name, 0) ? name : string.Concat("_", name);
3235
if (NameOptions[0]) name = UFString(name);
@@ -35,18 +38,21 @@ public string GetCsName(string name) {
3538
if (NameOptions[3]) name = string.Join("", name.Split('_').Select(a => UFString(a)));
3639
return name;
3740
}
38-
public string UFString(string text) {
41+
public string UFString(string text)
42+
{
3943
text = Regex.Replace(text, @"[^\w]", "_");
4044
if (text.Length <= 1) return text.ToUpper();
4145
else return text.Substring(0, 1).ToUpper() + text.Substring(1, text.Length - 1);
4246
}
43-
public string LFString(string text) {
47+
public string LFString(string text)
48+
{
4449
text = Regex.Replace(text, @"[^\w]", "_");
4550
if (text.Length <= 1) return text.ToLower();
4651
else return text.Substring(0, 1).ToLower() + text.Substring(1, text.Length - 1);
4752
}
4853

49-
public string GetCsType(DbColumnInfo col) {
54+
public string GetCsType(DbColumnInfo col)
55+
{
5056
if (fsql.Ado.DataType == FreeSql.DataType.MySql)
5157
if (col.DbType == (int)MySqlDbType.Enum || col.DbType == (int)MySqlDbType.Set)
5258
return $"{this.GetCsName(this.FullTableName)}{this.GetCsName(col.Name).ToUpper()}{(col.IsNullable ? "?" : "")}";
@@ -181,9 +187,10 @@ public string GetColumnAttribute(DbColumnInfo col, bool isInsertValueSql = false
181187

182188
if (dbinfo != null && dbinfo.isnullable != col.IsNullable)
183189
{
184-
if (col.IsNullable && fsql.DbFirst.GetCsType(col).Contains("?") == false && col.CsType.IsValueType)
190+
var cstype = fsql.DbFirst.GetCsType(col);
191+
if (col.IsNullable && cstype.Contains("?") == false && col.CsType.IsValueType)
185192
sb.Add("IsNullable = true");
186-
if (col.IsNullable == false && fsql.DbFirst.GetCsType(col).Contains("?") == true)
193+
if (col.IsNullable == false && (cstype.Contains("?") == true || cstype == "string"))
187194
sb.Add("IsNullable = false");
188195
}
189196

@@ -200,7 +207,7 @@ public string GetColumnAttribute(DbColumnInfo col, bool isInsertValueSql = false
200207
}
201208
}
202209
//else
203-
//sb.Add("CanInsert = false");
210+
//sb.Add("CanInsert = false");
204211
}
205212
}
206213
if (sb.Any() == false) return null;
@@ -260,8 +267,10 @@ public string GetMySqlEnumSetDefine()
260267
{
261268
if (fsql.Ado.DataType != FreeSql.DataType.MySql && fsql.Ado.DataType != FreeSql.DataType.OdbcMySql) return null;
262269
var sb = new StringBuilder();
263-
foreach (var col in table.Columns) {
264-
if (col.DbType == (int)MySqlDbType.Enum || col.DbType == (int)MySqlDbType.Set) {
270+
foreach (var col in table.Columns)
271+
{
272+
if (col.DbType == (int)MySqlDbType.Enum || col.DbType == (int)MySqlDbType.Set)
273+
{
265274
if (col.DbType == (int)MySqlDbType.Set) sb.Append("\r\n\t[Flags]");
266275
sb.Append($"\r\n\tpublic enum {this.GetCsName(this.FullTableName)}{this.GetCsName(col.Name).ToUpper()}");
267276
if (col.DbType == (int)MySqlDbType.Set) sb.Append(" : long");
@@ -272,10 +281,12 @@ public string GetMySqlEnumSetDefine()
272281
int unknow_idx = 0;
273282
string exp2 = string.Concat(col.DbTypeTextFull);
274283
int quote_pos = -1;
275-
while (true) {
284+
while (true)
285+
{
276286
int first_pos = quote_pos = exp2.IndexOf('\'', quote_pos + 1);
277287
if (quote_pos == -1) break;
278-
while (true) {
288+
while (true)
289+
{
279290
quote_pos = exp2.IndexOf('\'', quote_pos + 1);
280291
if (quote_pos == -1) break;
281292
int r_cout = 0;
@@ -284,7 +295,8 @@ public string GetMySqlEnumSetDefine()
284295
// else break;
285296
//}
286297
while (exp2[++quote_pos] == '\'') r_cout++;
287-
if (r_cout % 2 == 0/* && quote_pos - first_pos > 2*/) {
298+
if (r_cout % 2 == 0/* && quote_pos - first_pos > 2*/)
299+
{
288300
string str2 = exp2.Substring(first_pos + 1, quote_pos - first_pos - 2).Replace("''", "'");
289301
if (Regex.IsMatch(str2, @"^[\u0391-\uFFE5a-zA-Z_\$][\u0391-\uFFE5a-zA-Z_\$\d]*$"))
290302
slkdgjlksdjg += ", " + str2;

0 commit comments

Comments
 (0)