Thursday, June 4, 2020

.Net Core 3.1 with oData code only

for notes with explanation.

Startup.cs

using System;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Builder;
using OdataDemo.Models;
using Microsoft.OData.Edm;

namespace OdataDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

.Net Core 3.1 with oData

I have been cracking my head trying to solve following mysteries.
"@odata.count" is not show in the return result even after using $count=true.

odata is returning a list of object instead of in the odata format:

this:
 [
{"id":"e313596c-8fe9-4db6-b7cb-84f2e059447b","name":"Cody Allen","score":130},{"id":"349a4642-82bd-440f-a05b-ed27ed60cade","name":"Viral Pandya","score":140},{"id":"44fe2696-5285-470f-8b4c-ad983e5241b4","name":"student3","score":120},
{"id":"f2e9d6b7-6825-435b-a86a-37ac89ef77cb","name":"student4","score":150},
{"id":"3a4982f3-a9f3-4bc7-8886-315e7082fd59","name":"student5","score":100}
]

instead of this:
{
"@odata.context":"https://localhost:44372/odata/$metadata#Students",
"@odata.count":5,
"value":[
{"Id":"445bb0cf-efe8-4420-af43-75b37d81dc13","Name":"Cody Allen","Score":130},{"Id":"ec9acefd-1b38-4a0e-aba0-de9949f6690f","Name":"Viral Pandya","Score":140},{"Id":"675ba6a5-c799-4df6-a820-15cb274c33a1","Name":"student3","Score":120},
{"Id":"6fec0f6c-60b6-4624-ad11-38443e0e077f","Name":"student4","Score":150},
{"Id":"a6eeeeb1-8d55-4418-9ab8-4df448a6e1f0","Name":"student5","Score":100}
]}

Friday, May 8, 2020

Read appsettings.json in .net Core application with Dependency Injection (DI)

If you are truggling on how to read a constant setting or any setting value into a custom class / or any POCO class from the appsettings, and passing them back to the application that use .netCore and support DependencyInjection, then you are looking at the right post.

I have been struggling on this and here is what I found out.

In your webApp project, go to the Startup.cs, go to the method ConfigureServices, and add the following code:
it can be:
            var identitySettingsSection = Configuration.GetSection("AppIdentitySettingsConfig");
            services.Configure<AppIdentitySettingsConfig>(identitySettingsSection);

or
            services.Configure<AppIdentitySettingsConfig>(options => Configuration.GetSection("AppIdentitySettingsConfig").Bind(options));

Both are working same / fine. It will be looking like this:

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<AppIdentitySettingsConfig>(options => Configuration.GetSection("AppIdentitySettingsConfig").Bind(options));

Important note is make sure the parameter after GetSection is exactly the same as per your config file.

Next are how i set up for the other files.

Thursday, May 2, 2019

Copy file with command prompt

My colleague shared me a good trick on how you can copy a file with cmd, means you can do it even if it is half way being written by a program. But you will only have the content as per the moment it is duplicated.

1) C:\yourUsers > pushd \\TheFolderPath
2) D:\THePath> type fileName.txt > newFileName.txt

DONE!

Finding a table based on the table name in MSSQL

Here a code I googled on how you can find a table based on its name.

create table #t (
    DBName sysname not null
)
go
exec sp_MSforeachdb 'use [?]; if OBJECT_ID(''dbo.mytable'') is not null insert into #t (DBName) select ''?'''
go
select * from #t
go
drop table #t

Monday, April 15, 2019

Selecting all columns and alter or replace one column for all row

if you would like to do a select all and altering a column without visiting each element or
select * replace a column value without looping , here are what you can do

a very good post in stackOverflow can be found here:
in an Ienumerable list / the Linq way:
1) collection.Select(c => {c.PropertyToSet = value; return c;}).ToList();
2) collection.ToList().ForEach(c => c.PropertyToSet = value);

in data table:
make the good use of the .Expression , you can find MSDN documentation here :
1) column.Expression = "price * 0.0862";
2) column.Expression = anotherColumn's Name;

Saturday, October 24, 2015

CVS Vs GIT Vs TFS

As we know, three of them are for source version control.
Long time ago i compared some source controls in this post . I felt that it will be nice if I can further write my opinion on them.

CVS and GIT having free version.
TFS need payment.

In my opinion:

CVS are very straight forward and easy to use. You can set it up and check in the code, with 1 step, your partner can get your source code. But cvs is very dangerous, because it doesn't warn you for conflicts. It can be useful if you always have the major all time mighty latest powerful code in your local that you can save to overwrite other code. Else, it can be disaster when you end up spending hours to debug just to finally noticed someone overwritten the code. Tracing can be really easy because you just need to right click, select cvs -> history. then you can see the history of this file's changes. BUT, again but, it can be not helpful if you what to know what else you had checked in regarding to the same working item. Comparison of code is available. select the history, click on two versions, ( can be version 1 and version 10, hold alt key for the second selection, but only compare 2 files at 1 time.) selection option: compare.  

Delete database from azure and stop paying for database usage in azure

I have a hard time trying to temporary shut the database offline so that I don't have to pay for my database that serve as testing purpose. Unfortunately, what I found online is there is not temporary shut down in azure database either!

What can we do is delete the database. As long as your database doesn't store any data, you will not need to pay 1 cent. If you have a data, the payment is per hour based on the data size.

I cannot find delete database in Azure (manage.windowsazure.com)  sql databases.
A lot of post suggest to use the SqlAzureExtension , but the database can be delete without using any extension. It is pretty simple, just log on to your sql management studio, connect to the azure server and delete it as the same way you delete any database. That's it?? YES! I am happy because due to slow network that causing me hard time to download and search the azureExtension, I found this easy solution.
:D

Cheers,
Happy clouding.

Saturday, August 22, 2015

How to start MSDTC (Fix MSDTC not available error)

you can set it here:
START > SETTINGS > CONTROL PANEL > ADMINISTRATIVE TOOLS > SERVICES. Find the service called 'Distributed Transaction Coordinator' and RIGHT CLICK (on it and select) > Start.

or create a .dat file with this line of command:
net start msdtc
and run it

or
Administrative tools > Component services>component services > my computer > distributed transaction Coordinator > Local DTC > right click , propertires, security tab > Security settings > check the transaction manager communication.

Tuesday, August 11, 2015

Bootstrap

a.k.a twitter bootstrap.
This is one of the most famous platform recently, mainly because it fully the single responsive design.
Responsive design means you will see your web design as a fluid and the browser or device as a container. The fluid will fit into any container that you fill them to. With the rapid grow of internet, a lot of web wants to emerge in the shortest time in most of the device available.
This platform attract a lot of user because of the flexibility in fitting all device that we have, deskstop , mobile and tablet.