为什么初次建表时值对象并没有拆成字段呢? 返回

SqlSugar 沟通中
2 169

我在EntityService中配置一个作为值对象的属性的IsOwnsOne为true,然后以CodeFirst建表时,这个值对象的字段并没有拆散到表中,这是为什么呢?

我上传了一个Demo,这个很小,就一个program.cs文件,代码也粘贴在下面。

Temp.7z

using SqlSugar;
namespace Temp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddAuthorization();
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            //注入sql sugar
            builder.Services.AddSingleton<ISqlSugarClient>(serviceProvider =>
            {
                SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.MySql,
                    ConnectionString = "server=localhost;port=3306;user=root;password=123456;database=testDb",
                    IsAutoCloseConnection = true,
                    ConfigureExternalServices = new ConfigureExternalServices()
                    {
                        EntityService = (property, column) =>
                        {
                            if (property.PropertyType == typeof(UnitAddressadfafa))
                                column.IsOwnsOne = true;
                        }
                    }
                }
               );
                return sqlSugar;
            }
);


            var app = builder.Build();

            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();

            app.MapGet("/CreateDb", (ISqlSugarClient db) =>
            {
                db.DbMaintenance.CreateDatabase();
                db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(UnitCustomeradfafas));
            });

            app.Run();
        }
    }

    //下面两个类复制自sql sugar官网的值对象的例子
    public class UnitAddressadfafa
    {
        public string Street { get; set; }
        public string City { get; set; }
        public string ZipCode { get; set; }

    }
    public class UnitCustomeradfafas
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
        public UnitAddressadfafa Address { get; set; }
    }

}

在Swagger页面可以成功调用API,用navicat查看数据库也能看到成功创建了数据库和表,但是表中有Address字段确没有它的三个属性:

image.png

这是为什么呢?

热忱回答2

  • 这个我想一下怎么处理,这个是在事件之前要处理好。

    0 回复
  • SqlSugarCore 5.1.4.176-preview14


    勾预览,已修复

    0 回复