我希望将动态连接字符串传递给实体框架上下文.我有超过150个相同的模式(每个帐户一个),我想选择这样的连接:
ApplicationDbContext db = new ApplicationDbContext("dbName");
Run Code Online (Sandbox Code Playgroud)
理论上这很容易,因为我可以创建一个connectionString并将其作为构造函数的参数传递,例如:
public ApplicationDbContext(string dbName) : base(GetConnectionString(dbName))
{
}
public static string GetConnectionString(string dbName)
{
// The connectionString passed is something like:
// Server=localhost;Database={0};Uid=username;Pwd=password
var connString = ConfigurationManager
.ConnectionStrings["MyDatabase"]
.ConnectionString
.ToString();
return String.Format(connString, dbName);
}
Run Code Online (Sandbox Code Playgroud)
我只是传递连接字符串名称时可以成功连接,但是当我动态生成它时不能如下所示.我现在意识到这是因为web.config中的连接字符串中包含providerName="MySql.Data.MySqlClient"属性.
当我将实际连接字符串动态传递给连接时,它假定它需要连接到SQL Server而不是MySQL,并且由于连接字符串无效而失败.
问题是,如果我动态创建提供者名称,如何将提供者名称传递给连接字符串?
我有一个指令,我用于最近重构的表单验证样板.请允许我在扩展之前进一步解释该指令.
指令用法:
<form class="form-horizontal" name="personalDetails" validated-form>
<!-- Pass buttons into form through here -->
<a href="" class="btn btn-success"
data-ng-click="saveDetails()"
data-ng-disabled="!personalDetails.$valid">Save Changes</a>
</form>
Run Code Online (Sandbox Code Playgroud)
以前,我的指令看起来像这样,并且它有效.
app.directive('validatedForm', function($compile, $sce) {
return {
restrict: 'A',
scope: true,
link: function(scope, element, attrs) {
var template = //... HTML boilerplate code
var buttons = element.html(); // Get contents of element before overriding
element.html(template + buttons);
$compile(element.contents())(scope);
}
}
});
Run Code Online (Sandbox Code Playgroud)
html模板变得凌乱,我想把按钮"包含在模板里面",而不是在它们之后.所以我重构了我认为更好的指令.
app.directive('validatedForm', ['$compile', '$sce', function ($compile, $sce) {
var domContent = null;
return {
restrict: …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个角度服务来与PHP制作的简单REST服务器进行对话.我可以看到我能够获得单个记录,所有记录的列表,并添加新记录,但是,在添加新记录后,我遇到了从服务器获取正确响应以便能够对其进行操作的问题.
我知道它正在工作,因为新记录正在添加,但是,如果请求无效,我希望为用户发出通知,等等......
这是服务:
angular.module('adminApp.services', ['ngResource'])
.factory('Settings', function($resource) {
return $resource('rest/setting/:id', {id: '@id'}, {
'query' : { method: 'GET', params: {}, format: 'json', isArray: true },
'save' : { method: 'POST', params: {}, format: 'json', isArray: true },
'get' : { method: 'GET', params: {}, format: 'json', isArray: false },
'update': { method: 'PUT', params: {id: '@id'}, format: 'json', isArray: true },
'delete': { method: 'DELETE', params: {id: '@id'}, format: 'json', isArray: false }
});
});
Run Code Online (Sandbox Code Playgroud)
作为控制器的一部分,我有以下内容:
$scope.save = function() { …Run Code Online (Sandbox Code Playgroud)