Adding database in Azure –part 26
Now getting the database – needed for historic data.
When I go to Create New Database in Azure Portal , there are many types of Database (mongo, postgres, many others) – the easy choice, for me, is SqlServer ( even if the tables are not be updated – the exchange rates does not change a lot). There is something that is easy to deploy a WebApp : WebApp + Sql
But I wil create a new one – Azure Sql Managed Instance – the price for 4 vCore is > 400 EUR. Sure do not want this…
So I decided yo have SqlServer, with a database with 2 GB storage – smaller price, like 4.21 per month– for a normal person.
I can access with SSMS to create tables – and then I generate the script
CREATE TABLE [dbo].[NBR](
[ExchangeFrom] [nvarchar](50) NOT NULL,
[ExchangeTo] [nvarchar](50) NOT NULL,
[Date] [date] NOT NULL,
[ExchangeValue] [decimal](18, 6) NOT NULL,
CONSTRAINT [PK_NBR] PRIMARY KEY CLUSTERED
(
[ExchangeFrom] ASC,
[ExchangeTo] ASC,
[Date] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Create an ISave interface ( trying to cope with CQRS) .
Now I want to scaffold C# from database like https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
Installing
dotnet tool install –global dotnet-ef
gives error about versioning
dotnet tool install –global dotnet-ef –version 3.0.0
SUccess!
Now trying to scaffold
dotnet ef dbcontext scaffold “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -o Models
Error – does not find project
Ok. Run in the folder with the project. Now it does not like .NET Standard – wants .NET Core . Changed in .csproj <TargetFramework>netcoreapp3.0</TargetFramework>
Now it says:
Your startup project ‘InfovalutarDB’ doesn’t reference Microsoft.EntityFrameworkCore.Design.
Adding the NuGet Package
Now the command line
dotnet ef dbcontext scaffold “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -o Models
Works!
Reading about storing connection strings safe:
http://go.microsoft.com/fwlink/?LinkId=723263
Figuring a way to use either inmemory database( for fast testing) or sqlserver
var config = ConfigurationManager.ConnectionStrings[“DB”];
var opt= new DbContextOptionsBuilder<InfoValutarContext>();
if (config == null)
{
opt.UseInMemoryDatabase(“write”);
}
else
{
opt.UseSqlServer(config.ConnectionString);
}var cnt = new InfoValutarContext(opt.Options);
So a new Test created:
ISave s = new SaveSqlServer();
var response = await File.ReadAllTextAsync(Path.Combine(“Data”, “20191020bnr.txt”));
var m = new MockHttpMessageHandler();
m.When(“https://www.bnr.ro/nbrfxrates.xml”)
.Respond(“application/text”, response);var nbr = new GetNBRExchange(m);
var data = await nbr.GetActualRates().ToArrayAsync();
var nr= await s.Save(data);
Assert.Equal(nr, data.Length);
And modified in the Docker file to restore this new project
Infovalutar
And one hour passes...(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )
Leave a Reply