mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 优化 枚举属性的默认值容错,枚举下标不存在 0 的时候,mysql 迁移结构默认值报错;
This commit is contained in:
		@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Text;
 | 
				
			||||||
 | 
					using Xunit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace FreeSql.Tests.MySqlConnector
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public class UnitTest1
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public class TestAddEnum
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            public Guid Id { get; set; }
 | 
				
			||||||
 | 
					            public TestAddEnumType Type { get; set; }
 | 
				
			||||||
 | 
					            public PermissionTypeEnum TestType { get; set; }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        public enum TestAddEnumType { 中国人, 日本人 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public enum PermissionTypeEnum
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /// <summary>
 | 
				
			||||||
 | 
					            /// 菜单
 | 
				
			||||||
 | 
					            /// </summary>
 | 
				
			||||||
 | 
					            Menu = 1,
 | 
				
			||||||
 | 
					            /// <summary>
 | 
				
			||||||
 | 
					            /// 接口
 | 
				
			||||||
 | 
					            /// </summary>
 | 
				
			||||||
 | 
					            Api = 2
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void Test1()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            g.mysql.Select<TestAddEnum>().ToList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -408,10 +408,18 @@ namespace FreeSql.Tests
 | 
				
			|||||||
            public Guid? Id { get; set; }
 | 
					            public Guid? Id { get; set; }
 | 
				
			||||||
            public string xxx { get; set; }
 | 
					            public string xxx { get; set; }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        public class TestAddEnum
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            public Guid Id { get; set; }
 | 
				
			||||||
 | 
					            public TestAddEnumType Type { get; set; }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        public enum TestAddEnumType { 中国人, 日本人 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void Test1()
 | 
					        public void Test1()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            g.mysql.Select<TestAddEnum>().ToList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            g.sqlite.Insert(new TestGuidId { xxx = "111" }).ExecuteAffrows();
 | 
					            g.sqlite.Insert(new TestGuidId { xxx = "111" }).ExecuteAffrows();
 | 
				
			||||||
            g.sqlite.Insert(new TestGuidId { xxx = "222" }).ExecuteAffrows();
 | 
					            g.sqlite.Insert(new TestGuidId { xxx = "222" }).ExecuteAffrows();
 | 
				
			||||||
            var gkkdk1 = g.sqlite.Select<TestGuidId>().Where(a => true).ToList();
 | 
					            var gkkdk1 = g.sqlite.Select<TestGuidId>().Where(a => true).ToList();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -153,6 +153,19 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                    continue;
 | 
					                    continue;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                if (entityDefault != null) colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(entityDefault);
 | 
					                if (entityDefault != null) colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(entityDefault);
 | 
				
			||||||
 | 
					                if (p.PropertyType.IsEnum)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    var isEqualsEnumValue = false;
 | 
				
			||||||
 | 
					                    var enumValues = Enum.GetValues(p.PropertyType);
 | 
				
			||||||
 | 
					                    for (var a = 0; a < enumValues.Length; a++)
 | 
				
			||||||
 | 
					                        if (object.Equals(colattr.DbDefautValue, enumValues.GetValue(a)))
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            isEqualsEnumValue = true;
 | 
				
			||||||
 | 
					                            break;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    if (isEqualsEnumValue == false)
 | 
				
			||||||
 | 
					                        colattr.DbDefautValue = enumValues.Length > 0 ? enumValues.GetValue(0) : null;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                if (colattr.DbDefautValue != null && p.PropertyType != colattr.MapType) colattr.DbDefautValue = Utils.GetDataReaderValue(colattr.MapType, colattr.DbDefautValue);
 | 
					                if (colattr.DbDefautValue != null && p.PropertyType != colattr.MapType) colattr.DbDefautValue = Utils.GetDataReaderValue(colattr.MapType, colattr.DbDefautValue);
 | 
				
			||||||
                if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue;
 | 
					                if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue;
 | 
				
			||||||
                if (colattr.IsNullable == false && colattr.DbDefautValue == null)
 | 
					                if (colattr.IsNullable == false && colattr.DbDefautValue == null)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user