我正在使用ASP.NET5的Web API和EF7构建后端服务来设置多租户数据库结构.要求如下:
我现在面临以下挑战:
为了使服务能够动态添加或删除租户,我当前的实现基于JSON配置文件,该文件包含键值对中的所有租户连接字符串,如下所示:
{
"Tenants": [
{ "Tenant1": "Server=.\\SQLEXPRESS;Database=Tenant1;integrated security=True;" },
{ "Tenant2": "Server=.\\SQLEXPRESS;Database=Tenant2;integrated security=True;" }
]
}
Run Code Online (Sandbox Code Playgroud)
然后使用此配置来设置ContextFactory.此工厂使用DbContextOptions存储,以便在需要时动态创建DbContext实例,从而实现必要的短生命周期.工厂定义如下:
public class TenantContextFactory : ITenantContextFactory
{
/// <summary>
/// The tenant configurations store.
/// </summary>
private IDictionary<string, DbContextOptions> tenants;
/// <summary>
/// Creates a new TenantContextFactory
/// </summary>
public TenantContextFactory()
{
tenants = new Dictionary<string, DbContextOptions>();
}
/// <summary>
/// Registers a tenant configuration with the store.
/// </summary>
/// <param name="id">The tenant …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2015中的TypeScript中的ASP.NET 5和AngularJS构建应用程序.但是,即使在通过NuGet为AngularJS安装AngularJS Core和DefinitelyTyped TS引用之后,我仍然没有获得AngularJS库的IntelliSense.
我也使用ReSharper 9.1 AngularJS扩展,它在HTML文件中提供了IntelliSense,但在我的TypeScript代码中没有.
我也注意到即使存在对AngularJS和定义文件的引用,文件本身也无法在我的解决方案中找到.有人知道如何解决这个问题吗?
编辑:我找到了解决方案.如果你遇到同样的问题,请阅读我的答案.
我希望我的Web API能够通过字符串参数对其输出进行排序,例如:
http://myapi.com/api/people?skip=0&take=50&orderBy=lastName&descending=true.
因为我也有分页支持(skip和take我的API),我想orderBy和descending参数直接应用于SQL查询,使正确的结果来自于数据库.
但是,在执行此操作时,在尝试将参数orderBy与我希望通过字符串比较排序的类的实际属性进行匹配时,代码可能变得非常难以管理.
我找到了一个解决方案,该解决方案应该与LINQ to Entities一起工作,因此也适用于新的EF7,但是当我尝试使用新的Core CLR编译此代码时,我收到以下消息:
错误CS1503参数2:无法从'System.Linq.Expressions.Expression>'转换为'string'
失败的解决方案的代码是OrderBy<T>方法:
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
{
return source.OrderBy(ToLambda<T>(propertyName));
}
Run Code Online (Sandbox Code Playgroud)
似乎新的Core CLR不支持这种尝试.有没有其他方法可以使解决方案与新的CLR一起使用?如果不是,我还有什么其他选择来使用EF7进行排序而不会导致无数if或switch语句将输入字符串与属性名称进行比较?