iXserverLoopEnv/tools/CreateLoopDb.cs

56 lines
1.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 按 iXserver 的 EDMX 建一个空库,与 FeatureTest 的 TestBase.Setup 同一条路(Database.Create)。
// 这样表结构永远跟当前部署的 Model.dll 一致,不用另外维护一份 schema.sql。
//
// loop.ps1 reset 用 .NET Framework 自带的 csc 现编现用:
// 引用 run\ixserver\bin 里的 Model.dll / EntityFramework.dll,连接串由同名 .exe.config 给。
// Model 的实体还引用了 bin 里别的程序集(WebChat.Message 等),所以第一个参数传 bin 目录,从那里解析依赖。
// 库已存在时直接失败——删库由 loop.ps1 负责,这里不做任何破坏性操作。
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
static class CreateLoopDb
{
static int Main(string[] args)
{
if (args.Length != 1 || !Directory.Exists(args[0]))
{
Console.Error.WriteLine("usage: CreateLoopDb <run\\ixserver\\bin>");
return 64;
}
var bin = args[0];
AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
{
var path = Path.Combine(bin, new AssemblyName(e.Name).Name + ".dll");
return File.Exists(path) ? Assembly.LoadFrom(path) : null;
};
try
{
return Create();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
return 1;
}
}
// 单独一个方法:Main 被 JIT 时还没挂上 AssemblyResolve,不能在 Main 里直接碰 Model 的类型
[MethodImpl(MethodImplOptions.NoInlining)]
static int Create()
{
using (var context = new Model.iXsocialEntities())
{
if (context.Database.Exists())
{
Console.Error.WriteLine("database already exists, drop it first");
return 2;
}
context.Database.Create();
}
Console.WriteLine("created");
return 0;
}
}