初始化数据库结果跟预期结果不一样 返回

SqlSugar 沟通中
3 218
该叫什么 ABC 发布于2周前
悬赏:0 飞吻

我想通过CreateDatabase和InitTables把TEST1表初始在TEST1的数据库里,TEST2表初始华在TEST2的数据库里。

但是最终的结果是只创建了TEST1的数据库,,TEST1表和TEST2表都创建在了TEST1的数据库里。

Demo代码如下:

请问应该怎么调整?


using SqlSugar;
using SqlSugar.IOC;

namespace sugarTest
{
    /// <summary>
    /// TEST
    /// </summary>
    [SugarTable("TEST1", "TEST1")]
    [Tenant("TEST1")]
    public partial class TEST1
    {
        ///<summary>
        /// 主键
        ///</summary>
        [SugarColumn(IsPrimaryKey = true,Length = 50, ColumnDescription = "主键")]
        //[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public string ID { get; set; } // Id (Primary key)

        ///<summary>
        /// 是否为图片(是/否)
        ///</summary>
        [SugarColumn(Length = 20, ColumnDescription = "是否为图片(是/否)")]
        public string IS_IMG { get; set; } // IsImg

         
    }

    /// <summary>
    /// TEST
    /// </summary>
    [SugarTable("TEST2", "TEST2")]
    [Tenant("TEST2")]
    public partial class TEST2
    {
        ///<summary>
        /// 主键
        ///</summary>
        [SugarColumn(IsPrimaryKey = true, Length = 50, ColumnDescription = "主键")]
        //[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public string ID { get; set; } // Id (Primary key)

        ///<summary>
        /// 是否为图片(是/否)
        ///</summary>
        [SugarColumn(Length = 20, ColumnDescription = "是否为图片(是/否)")]
        public string IS_IMG { get; set; } // IsImg


    }


    internal class Program
    {
        static string conn1 = "Data Source=.;Initial Catalog=test1;Persist Security Info=True;User ID=sa;Password=123456;Encrypt=True;TrustServerCertificate=True;";
        static string conn2 = "Data Source=.;Initial Catalog=test2;Persist Security Info=True;User ID=sa;Password=123456;Encrypt=True;TrustServerCertificate=True;";
        static IocDbType _iocDbType = IocDbType.SqlServer;
        static void Main(string[] args)
        {


            List<IocConfig> iocList = new List<IocConfig>
            {
                new IocConfig()
                {
                    ConfigId="TEST1",
                    ConnectionString = conn1,
                    DbType = _iocDbType,
                    IsAutoCloseConnection = true,
                },
                new IocConfig()
                {
                    ConfigId="TEST2",
                    ConnectionString = conn2,
                    DbType = _iocDbType,
                    IsAutoCloseConnection = true,
                }
            };
            SugarIocServices.AddSqlSugar(iocList);

            var db = DbScoped.SugarScope;
            //建库:如果不存在创建数据库存在不会重复创建 
            db.DbMaintenance.CreateDatabase();// 注意 :Oracle和个别国产库需不支持该方法,需要手动建库 
            db.CodeFirst.InitTables(typeof(TEST1));
            db.CodeFirst.InitTables(typeof(TEST2));
            Console.WriteLine("Hello, World!");
        }
    }
}



热忱回答3

  •  db.CodeFirst.GetConnection("TEST1").InitTables(typeof(TEST1));
     db.CodeFirst.GetConnection("TEST2").InitTables(typeof(TEST2));


    0 回复
  • 或者

     
    db.CodeFirst.InitTablesWithAttr(type,type2)


    0 回复
  • ABC ABC VIP0
    2周前

    @fate sta

    应该这样子写

      db.GetConnection("TEST1").DbMaintenance.CreateDatabase();

      db.GetConnection("TEST2").DbMaintenance.CreateDatabase();

      db.GetConnection("TEST1").CodeFirst.InitTables(typeof(TEST1));

      db.GetConnection("TEST2").CodeFirst.InitTables(typeof(TEST2));


    0 回复