示例#1
0
        public void nou_null_guid_defaults_to_empty_guid()
        {
            DbModelType uniqueidentifier = _dbTypes["uniqueidentifier"].Copy();

            uniqueidentifier.Value = null;
            Assert.That(uniqueidentifier.ToDDLValue(false), Is.EqualTo("'00000000-0000-0000-0000-000000000000'"));
        }
示例#2
0
        public void nou_null_VARBINARY_defaults_to_0()
        {
            DbModelType notNullVarbinary = _dbTypes["varbinary"].Copy();

            notNullVarbinary.Value = null;
            Assert.That(notNullVarbinary.ToDDLValue(false), Is.EqualTo("0"));
        }
示例#3
0
        public void nou_null_int_defaults_to_0()
        {
            DbModelType notNullNum = _dbTypes["int"].Copy();

            notNullNum.Value = null;
            Assert.That(notNullNum.ToDDLValue(false), Is.EqualTo("0"));
        }
示例#4
0
        public void not_null_nvarchar_defaults_to_empty_quotes()
        {
            DbModelType notNullNvarchar = _dbTypes["nvarchar"].Copy();

            notNullNvarchar.Value = null;
            Assert.That(notNullNvarchar.ToDDLValue(false), Is.EqualTo("N''"));
        }
示例#5
0
        public void datetime_as_null()
        {
            DbModelType nullDate = _dbTypes["datetime"].Copy();

            nullDate.Value = null;
            Assert.That(nullDate.ToDDLValue(), Is.EqualTo("null"));
        }
示例#6
0
        public void int_with_value()
        {
            DbModelType num = _dbTypes["int"].Copy();

            num.Value = 55;
            Assert.That(num.ToDDLValue(), Is.EqualTo("55"));
        }
示例#7
0
        public void nvarchar_with_value()
        {
            DbModelType str = _dbTypes["nvarchar"].Copy();

            str.Value = "the quick brown fox...";
            Assert.That(str.ToDDLValue(), Is.EqualTo("N'the quick brown fox...'"), "Should take the literals into account");
        }
示例#8
0
        public void nou_null_BIT_defaults_to_0()
        {
            DbModelType notNullBit = _dbTypes["bit"].Copy();

            notNullBit.Value = null;
            Assert.That(notNullBit.ToDDLValue(false), Is.EqualTo("0"));
        }
示例#9
0
        public void Decimal_type()
        {
            DbModelType dbType = _dbTypes["decimal"];

            Assert.That(dbType.Name, Is.EqualTo("decimal").IgnoreCase);
            Assert.That(dbType.CreateFormat, Is.EqualTo("decimal({0}, {1})"));
            // todo Assert.That(dbType.SystemType, Is.EqualTo(typeof(decimal)));
        }
示例#10
0
        public void Float_type()
        {
            DbModelType dbType = _dbTypes["float"];

            Assert.That(dbType.Name, Is.EqualTo("float").IgnoreCase);
            Assert.That(dbType.Summary, Is.EqualTo("float"));
            Assert.That(dbType.SystemType, Is.EqualTo(typeof(double)));
        }
示例#11
0
        public void Money_type()
        {
            DbModelType dbType = _dbTypes["money"];

            Assert.That(dbType.Name, Is.EqualTo("money").IgnoreCase);
            Assert.That(dbType.Summary, Is.EqualTo("money"));
            Assert.That(dbType.SystemType, Is.EqualTo(typeof(decimal)));
        }
示例#12
0
        public void Summary_renders_a_decimal_as_default()
        {
            DbModelType dbType = new DbModelType("decimal", 26)
            {
                Precision = -1, Scale = -1, CreateFormat = "decimal({0}, {1})"
            };

            Assert.That(dbType.Summary, Is.EqualTo("decimal"));
        }
示例#13
0
        public void Summary_renders_uses_CreateFormat_over_name_if_present()
        {
            DbModelType dbType = new DbModelType("mytype", 100)
            {
                CreateFormat = "MyType"
            };

            Assert.That(dbType.Summary, Is.EqualTo("MyType"));
        }
示例#14
0
        public void Summary_renders_a_decimal_8_6()
        {
            DbModelType dbType = new DbModelType("decimal", 8 + 6)
            {
                Precision = 8, Scale = 6, CreateFormat = "decimal({0}, {1})"
            };

            Assert.That(dbType.Summary, Is.EqualTo("decimal(8, 6)"));
        }
示例#15
0
        public void Summary_renders_an_int()
        {
            DbModelType dbType = new DbModelType("int", 4)
            {
                CreateFormat = "int"
            };

            Assert.That(dbType.Summary, Is.EqualTo("int"));
        }
示例#16
0
        public void Summary_renders_a_varchar_10()
        {
            DbModelType dbType = new DbModelType("varchar", 10)
            {
                CreateFormat = "varchar({0})"
            };

            Assert.That(dbType.Summary, Is.EqualTo("varchar(10)"));
        }
示例#17
0
        public void LongText_type()
        {
            DbModelType dbType = _dbTypes["longtext"];

            Assert.That(dbType.Name, Is.EqualTo("LongText").IgnoreCase);
            Assert.That(dbType.Summary, Is.EqualTo("LongText"));
            Assert.That(dbType.LiteralPrefix, Is.EqualTo("'"));
            Assert.That(dbType.LiteralSuffix, Is.EqualTo("'"));
        }
示例#18
0
        public void VarBinary_type()
        {
            DbModelType dbType = _dbTypes["varbinary"];

            Assert.That(dbType.Name, Is.EqualTo("VarBinary").IgnoreCase);
            Assert.That(dbType.CreateFormat, Is.EqualTo("VarBinary({0})"));
            Assert.That(dbType.LiteralPrefix, Is.EqualTo("0x"));
            Assert.That(dbType.LiteralSuffix, Is.Empty);
        }
示例#19
0
        public string GetRelationConfig()
        {
            //当前类型的所有公共属性
            var allFields = DbModelType.GetProperties();

            //获取简单类型字段
            var simpleTypeFields = allFields.Where(x => !x.PropertyType.IsGenericType && !typeof(IDbModel).IsAssignableFrom(x.PropertyType));

            //导航属性字段,表示关系
            var navigationFields = allFields.Where(x => x.PropertyType.IsGenericType || typeof(IDbModel).IsAssignableFrom(x.PropertyType)).ToList();

            if (simpleTypeFields.Count() != 2 || navigationFields.Count() != 2)
            {
                throw new Exception($"实体{DbModelType}的导航属性配置不正确,在多对多的关系映射中,必须包含2个Model的Id以及对应的导航属性,如果不是多对多的映射,不需要继承自IRelationMap接口,直接遵守微软约定大约配置的法则EFCore即可自动配置.");
            }

            StringBuilder sb = new StringBuilder();

            //复合主键设置
            var mutliKey = $"builder.HasKey(t => new { "{" + string.Join(',', simpleTypeFields.Select(x => "t." + x.Name)) + "}" });";

            sb.AppendLine(mutliKey);

            var leftModelName = navigationFields[0].Name;

            var leftModelNavigations = navigationFields[0].PropertyType.GetProperties().Where(x => x.PropertyType.GenericTypeArguments.Any(n => n == DbModelType));

            if (leftModelNavigations.Count() != 1)
            {
                throw new Exception($"类型{navigationFields[0].PropertyType}中有且只能有一个泛型类型为{DbModelType}的导航属性");
            }

            var leftModelNavigationFieldName = leftModelNavigations.First().Name;

            var rightModelName = navigationFields[1].Name;

            var rightModelNavigations = navigationFields[1].PropertyType.GetProperties().Where(x => x.PropertyType.GenericTypeArguments.Any(n => n == DbModelType));

            if (rightModelNavigations.Count() != 1)
            {
                throw new Exception($"类型{navigationFields[0].PropertyType}中有且只能有一个泛型类型为{DbModelType}的导航属性");
            }

            var rightModelNavigationFieldName = rightModelNavigations.First().Name;

            //配置左边的一对多关系
            var leftConfig = $"builder.HasOne(t => t.{leftModelName}).WithMany(x => x.{leftModelNavigationFieldName}).HasForeignKey(t => t.{leftModelName+"Id"});";

            //配置右边的一对多关系
            var rightConfig = $"builder.HasOne(t => t.{rightModelName}).WithMany(x => x.{rightModelNavigationFieldName}).HasForeignKey(t => t.{rightModelName + "Id"});";

            sb.AppendLine(leftConfig);
            sb.AppendLine(rightConfig);

            return(sb.ToString());
        }
示例#20
0
        public void VarChar_type()
        {
            DbModelType dbType = _dbTypes["varchar"];

            Assert.That(dbType.Name, Is.EqualTo("VarChar").IgnoreCase);
            Assert.That(dbType.Length, Is.EqualTo(255));
            Assert.That(dbType.CreateFormat, Is.EqualTo("VarChar({0})"));
            Assert.That(dbType.LiteralPrefix, Is.EqualTo("'"));
            Assert.That(dbType.LiteralSuffix, Is.EqualTo("'"));
        }
示例#21
0
        public void NVarchar_type()
        {
            DbModelType dbType = _dbTypes["nvarchar"];

            Assert.That(dbType.Name, Is.EqualTo("nvarchar").IgnoreCase);
            Assert.That(dbType.Length, Is.EqualTo(4000));
            Assert.That(dbType.CreateFormat, Is.EqualTo("nvarchar({0})"));
            Assert.That(dbType.LiteralPrefix, Is.EqualTo("N'"));
            Assert.That(dbType.LiteralSuffix, Is.EqualTo("'"));
            // todo Assert.That(dbType.SystemType, Is.EqualTo(typeof(string)));
        }
示例#22
0
        public void DateTime_type()
        {
            DbModelType dbType = _dbTypes["datetime"];

            Assert.That(dbType.Name, Is.EqualTo("DateTime").IgnoreCase);
            Assert.That(dbType.Summary, Is.EqualTo("DateTime"));
            Assert.That(dbType.LiteralPrefix, Is.EqualTo("#"));
            Assert.That(dbType.LiteralSuffix, Is.EqualTo("#"));
            var copy = dbType.Copy();

            copy.Value = new DateTime(2000, 12, 17);
            Assert.That(copy.ToDDLValue(), Is.EqualTo("#17/12/2000 12:00:00 AM#"));
        }
示例#23
0
        private string OtherModel()
        {
            //当前类型的所有公共属性
            var allFields = DbModelType.GetProperties();

            //排除导航属性字段,这些字段是以关系的形式处理
            var simpleTypeFields = allFields.Where(x => !x.PropertyType.IsGenericType && !typeof(IDbModel).IsAssignableFrom(x.PropertyType));

            StringBuilder sb = new StringBuilder();

            simpleTypeFields.ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));

            return(sb.ToString());
        }
示例#24
0
        public string GetTableConfig()
        {
            var queryFilterConfig = "";

            //是否有软删除字段
            var hasIsDelete = DbModelType.GetProperties().Count(x => x.Name == "IsDelete") > 0;

            if (hasIsDelete)
            {
                queryFilterConfig = ".HasQueryFilter(x=> !x.IsDelete)";
            }
            var tabConfig = $"builder.ToTable(\"{DbModelType.Name.UpperCharToUnderLine()}\"){queryFilterConfig};";

            return(tabConfig);
        }
        public string GetFieldsConfig()
        {
            var baseFields = typeof(BaseModel).GetProperties();

            //取到自己的属性字段,不要BaseModel的
            var selfFields = DbModelType.GetProperties().Where(x => !baseFields.Any(dx => { return(dx.Name == x.Name); }));

            //排除导航属性字段,这些字段是以关系的形式
            var simpleTypeFields = selfFields.Where(x => !x.PropertyType.IsGenericType && !typeof(BaseModel).IsAssignableFrom(x.PropertyType));


            StringBuilder sb = new StringBuilder();

            sb.Clear();
            sb.AppendLine(GetFieldConfig(baseFields.Where(x => x.Name == "Id").First()));
            simpleTypeFields.ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));
            baseFields.Where(x => x.Name != "Id").ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));

            return(sb.ToString());
        }
示例#26
0
        private string InheritBaseModel()
        {
            var baseFields = typeof(BaseModel).GetProperties();

            //当前类型的所有公共属性
            var allFields = DbModelType.GetProperties();

            //排除BaseModel的属性
            var selfFields = allFields.Where(x => !baseFields.Any(dx => { return(dx.Name == x.Name); }));

            //排除导航属性字段,这些字段是以关系的形式处理
            var simpleTypeFields = selfFields.Where(x => !x.PropertyType.IsGenericType && !typeof(IDbModel).IsAssignableFrom(x.PropertyType));


            StringBuilder sb = new StringBuilder();

            sb.Clear();
            sb.AppendLine(GetFieldConfig(baseFields.Where(x => x.Name == "Id").First()));
            simpleTypeFields.ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));
            //排除在子类中被private的字段
            baseFields.Where(x => x.Name != "Id" && allFields.Any(n => { return(n.Name == x.Name); })).ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));

            return(sb.ToString());
        }
示例#27
0
        public void Summary_renders_a_the_type_name_if_there_is_no_CreateFormat()
        {
            DbModelType dbType = new DbModelType("decimal", 26);

            Assert.That(dbType.Summary, Is.EqualTo("decimal"));
        }