Skip to content

Commit

Permalink
Add new connection string under ConnectionStrings node in appsettings…
Browse files Browse the repository at this point in the history
….json

Fixes #259
Added a newline in controllerWithActions.cshtml. Fixes #228
  • Loading branch information
prafullbhosale committed Aug 26, 2016
1 parent 72d570c commit d48e28e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public EditSyntaxTreeResult EditStartupForNewContext(ModelType startUp, string d

string textToAddAtEnd =
statementLeadingTrivia + "{0}.AddDbContext<{1}>(options =>" + Environment.NewLine +
statementLeadingTrivia + " options.UseSqlServer({2}[\"Data:{1}:ConnectionString\"]));" + Environment.NewLine;
statementLeadingTrivia + " options.UseSqlServer({2}.GetConnectionString(\"{1}\")));" + Environment.NewLine;

if (configServicesMethod.Body.Statements.Any())
{
Expand Down Expand Up @@ -225,25 +225,18 @@ internal void AddConnectionString(string connectionStringName, string dataBaseNa
content = JObject.Parse(_fileSystem.ReadAllText(appSettingsFile));
}

string dataNodeName = "Data";
string connectionStringNodeName = "ConnectionString";
string connectionStringNodeName = "ConnectionStrings";

if (content[dataNodeName] == null)
if (content[connectionStringNodeName] == null)
{
writeContent = true;
content[dataNodeName] = new JObject();
content[connectionStringNodeName] = new JObject();
}

if (content[dataNodeName][connectionStringName] == null)
if (content[connectionStringNodeName][connectionStringName] == null)
{
writeContent = true;
content[dataNodeName][connectionStringName] = new JObject();
}

if (content[dataNodeName][connectionStringName][connectionStringNodeName] == null)
{
writeContent = true;
content[dataNodeName][connectionStringName][connectionStringNodeName] =
content[connectionStringNodeName][connectionStringName] =
string.Format("Server=(localdb)\\mssqllocaldb;Database={0};Trusted_Connection=True;MultipleActiveResultSets=true",
dataBaseName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

@{string modelName = (Model.ClassName.EndsWith("Controller")? Model.ClassName.Substring(0,Model.ClassName.Length-10): Model.ClassName);}
namespace @Model.NamespaceName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ public void AddConnectionString_Creates_App_Settings_File()

//Assert
string expected = @"{
""Data"": {
""MyDbContext"": {
""ConnectionString"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
""ConnectionStrings"": {
""MyDbContext"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
}";
var appSettingsPath = Path.Combine(AppBase, "appsettings.json");
Expand All @@ -112,52 +110,30 @@ public void AddConnectionString_Creates_App_Settings_File()
// Empty file with valid json token
[InlineData("{}",
@"{
""Data"": {
""MyDbContext"": {
""ConnectionString"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
""ConnectionStrings"": {
""MyDbContext"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
}")]
// File with no node for connection name
[InlineData(@"{
""Data"": {
}
}",
@"{
""Data"": {
""MyDbContext"": {
""ConnectionString"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
}
}")]
// File with node for connection name but no ConnectionString property
[InlineData(@"{
""Data"": {
""MyDbContext"": {
}
""ConnectionStrings"": {
}
}",
@"{
""Data"": {
""MyDbContext"": {
""ConnectionString"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
""ConnectionStrings"": {
""MyDbContext"": ""Server=(localdb)\\mssqllocaldb;Database=MyDbContext-NewGuid;Trusted_Connection=True;MultipleActiveResultSets=true""
}
}")]
// File with node for connection name and also existing ConnectionString property
// modification should be skipped in this case
[InlineData(@"{
""Data"": {
""MyDbContext"": {
""ConnectionString"": ""SomeExistingValue""
}
""ConnectionStrings"": {
""MyDbContext"": ""SomeExistingValue""
}
}",
@"{
""Data"": {
""MyDbContext"": {
""ConnectionString"": ""SomeExistingValue""
}
@"{
""ConnectionStrings"": {
""MyDbContext"": ""SomeExistingValue""
}
}")]
public void AddConnectionString_Modifies_App_Settings_File_As_Required(string previousContent, string newContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace WebAppNamespace
public void ConfigureServices(IServiceCollection servicesVar)
{
servicesVar.AddDbContext<MyContext>(options =>
options.UseSqlServer(ConfigurationProp["Data:MyContext:ConnectionString"]));
options.UseSqlServer(ConfigurationProp.GetConnectionString("MyContext")));
}
public IConfigurationRoot ConfigurationProp { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace WebAppNamespace
servicesVar.AddEntityFramework();

servicesVar.AddDbContext<MyContext>(options =>
options.UseSqlServer(ConfigurationProp["Data:MyContext:ConnectionString"]));
options.UseSqlServer(ConfigurationProp.GetConnectionString("MyContext")));
}
}
}

0 comments on commit d48e28e

Please sign in to comment.