EF Core with Postgres connection string error - asp.net-core

I am trying to use ASP.net Core along with postgres for the very first time. I was following (http://dotnetthoughts.net/using-postgresql-with-aspnet-core/) this blog post to create my first migration. I already created a database in my posgresSQL called asp_api_test.
My dotnet ef migrations add Initial was successful. However when I ran dotnet ef database update I ran into error Format of the initialization string does not conform to specification starting at index 0
My appsettings.json:
{
"ConnectionStrings": {
"DataAccessPostgreSqlProvider": "Host=localhost;Username=postgres;Password=root;Database=asp_trial_api;Pooling=true;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
I am guessing my connection string is incorrect.
My startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<WebAPIDataContext>(options => {
options.UseNpgsql("DataAccessPostgreSqlProvider", b => b.MigrationsAssembly("New_Api"));
});
services.AddMvc();
}
My project.json:
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview1-final",
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
},
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
"Microsoft.ApplicationInsights.AspNetCore": "2.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
What am I doing wrong?

I updated to following-
appsettings.js:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Host=localhost;Username={{postgres}};Password={{root}};Database={{asp_trial_api}}"
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
and in my startup.cs:
services.AddDbContext<WebAPIDataContext>(options => {
options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]);
});

Related

.Net Framework + Core Template application deploy to cloud Foundary

Good day All,I am working on a ASP.NET application core template with .Net framework. I have to publish my application to cloud foundry.But unfortunately my application is crashing and not able to deploy to cloud foundry environment.Anyone if come across the same situation and resolved the issue please suggest.Thanks. I have gone through some websites and tried modify as below still not able to deploy. I am using .NetCoreBuild pack for the deployment.
Program.cs:-
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
//.UseContentRoot(Directory.GetCurrentDirectory())
//.UseIISIntegration()
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
host.Run();
}
Project.Json:-
{
"buildOptions": {
"copyToOutput": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
],
"exclude": [
"bin",
"obj"
]
},
"debugType": "portable",
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
],
"exclude": [
"bin",
"obj"
]
},
"dependencies": {
"Microsoft.AspNetCore.Server.Kestrel": "1.0.2",
"Microsoft.AspNetCore.Routing": "1.0.2",
"Microsoft.AspNetCore.Mvc": "1.0.2",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview4-final",
"type": "build"
},
"Microsoft.AspNetCore.StaticFiles": "1.0.1",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.1",
"Microsoft.Extensions.Configuration.Json": "1.0.1",
"Microsoft.Extensions.Logging": "1.0.1",
"Microsoft.Extensions.Logging.Console": "1.0.1",
"Microsoft.Extensions.Logging.Debug": "1.0.1",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.3"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.1",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.Extensions.Configuration": "1.1.0"
},
"frameworks": {
"net461": { }
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"scripts": {
"precompile": [ "dotnet bundle" ]
},
"tools": {
"BundlerMinifier.Core": "2.2.301",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview4-final"
},
"version": "1.0.3"
}
In your project.json file you're specifying:
"frameworks": {
"net461": { }
}
The net461 refers to .NET Framework 4.6.1, but the .NET Core buildpack only supports .NET Core. Use netcoreapp1.0 instead of net461, like so:
"frameworks": {
"netcoreapp1.0": { }
}

Referencing .net 4.6.1 Library in .net Core

I am creating .net core WebApi and trying to use a library created previously using .net 4.6.1. I have created a NuGet package for that 4.6.1 library and able to add that to my NET Core application without any issue. But when I reference that library in my .netcore controller, I get message "you can use Navigation bar to switch context".
Here is my project.json:
{
"dependencies": {
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net461": {
"dependencies": {
"Db.Library": "1.0.0"
}
},
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
],
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
}
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
there is no "dnxcore50": { } used.
So, cannot remove that as suggested in other threads. What could be the solution?
Update:
The following link help me a lot :
http://www.hanselman.com/blog/HowToReferenceAnExistingNETFrameworkProjectInAnASPNETCore10WebApp.aspx
Now I created a new project with .netFramework and added my nuget package and able to compile.
Posting this, if it helps someone.

Cannot scaffold Controller / Views in ASP.NET Core

When I try to add a Scaffold Items using the code generator I get following error message:
Exception output while run the generator:
Currently i tried it many hours but i doesnt found the failure :(
Here is my project.json file
{
"dependencies": {
"Microsoft.AspNetCore.Authentication": "1.0.0",
"Microsoft.AspNetCore.Authorization": "1.1.0-*",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.1.0-*",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.1.0-*",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-*",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-preview1-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Design": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-*",
"Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-*",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0-*"
},
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview3-final",
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview3-final"
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview3-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Mvc": "1.0.0-preview3-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview3-final"
},
"frameworks": {
"netcoreapp1.1": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
You have a dependencie Microsoft.Extensions.CodeGenerators.Mvc and later Microsoft.VisualStudio.Web.CodeGenerators.Mvc. I think this with Microsoft.Extensions is false.
I mean, that your framework-definition is not correct.
In your project:
"frameworks": {
"netcoreapp1.1": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
Check this:
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
}
Then can it be, that you can now use Microsoft.Extensions.CodeGenerators.Mvc for dnx.

Is NodaTime compatible with NetCoreApp 1.0?

I have dotnet core app that I'm working on that is cross-platform. I wanted to use Noda as a utility in my project but get the following error even tho I have nodatime defined as a dependency in my project:
System.IO.FileNotFoundException: Could not load file or assembly 'NodaTimestrong text, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1'. The system cannot find the file specified.
File name: 'NodaTime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1'
at project.ef.CalendarHelper.ValidateTimeZone(String timezoneTypeName)
at project.ef.CalendarHelper.
Here are the project config files:
API PROJECT
{
"buildOptions": {
"preserveCompilationContext": true,
"emitEntryPoint": true,
"warningsAsErrors": true,
"debugType": "portable",
"copyToOutput": {
"include": [
"config.json",
"Certificate.pfx"
]
}
},
"dependencies": {
"AspNet.Security.OAuth.Introspection": "1.0.0-alpha2-final",
"AspNet.Security.OAuth.Validation": "1.0.0-alpha2-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0",
"Microsoft.AspNetCore.Mvc.Cors": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"type": "build",
"version": "1.0.0-preview2-final"
},
"Microsoft.NETCore.App": "1.0.0",
"project.ef": "1.0.0-*"
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [ "portable-dnxcore50+net45+win8+wp8+wpa81" ]
}
},
"publishOptions": {
"include": [
"config.json"
]
},
"runtimes": {
"win10-x64": {},
"osx.10.11-x64": {},
"ubuntu.14.04-x64": {}
}
}
EF PROJECT
{
"dependencies": {
"NETStandard.Library": "1.6.0",
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.1",
"Npgsql.EntityFrameworkCore.PostgreSQL.Design": "1.0.1",
"project.Internal.ERP": "1.1.0-*",
"project.models": "1.0.0-*",
"NodaTime": "2.0.0-alpha-*"
},
"frameworks": {
"netstandard1.6": {
"imports": [
"dnxcore50",
"portable-net451+win8"
]
}
}
}
Noda Time 1.x only targets PCLs. You may be able to get it to work with netcoreapp1.0, but I'm not going to guarantee it.
But Noda Time 2.0 targets netstandard1.1, so should be fine. That's only available as an alpha release right now, but it works fine:
project.json contents:
{
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"NodaTime": "2.0.0-alpha20160729"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
}
}
}
}
Program.cs contents:
using System;
using NodaTime;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(SystemClock.Instance.GetCurrentInstant());
}
}
That runs with no problems.

Can not add reference to .NET Core class library in ASP.NET Core project

I've created a new ASP.NET Core project and a class library for it in VS 2015 (Update 3).
This is how project.json looks like:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
And this is a project.json of the ASP.NET Project:
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Now I can't add my class library to my site as 'dependency can't be resolved'.
I didn't see a reference to your library in your application's dependencies section. If your library and your application are in the same solution, make sure you use target: project like so:
project.json of ASP.NET Core project:
"dependencies": {
...
"MyLibrary": {
"version": "1.0.0",
"target": "project"
}
}
Otherwise, NuGet will look for your library as a package instead of looking in the solution.