Get role/s of current logged in user in ASP.NET Core MVC

You may want to consider trying to load the actual ApplicationUser object via the FindByEmail() or some other method and passing that object into the GetRolesAsync() method as seen below : // Resolve the user via their email var user = await _userManager.FindByEmailAsync(model.Email); // Get the roles for the user var roles = await _userManager.GetRolesAsync(user); … Read more

Formatting ToShortDateString to dd/MM/yyyy

ToShortDateString does not have an overload which takes any parameter. If your ToShortDateString() returns MM/dd/yyyy format, that means your CurrentCulture has this format in it’s ShortDatePattern property. You can always use custom formatting for that like with proper culture like; TextBox2.Text = DateTime.Today.ToString(“dd/MM/yyyy”, CultureInfo.InvariantCulture);

Change or rename a column name without losing data with Entity Framework Core 2.0

EF Core creates its migrations by comparing your models to the current database snapshot (a c# class). It then uses this to create a migration file you can review. However, EF Core cannot always know if you replaced this column or created a new column. When you check your migration file, make sure there are … Read more

In C#, what is the difference between comparing two dates using tick and just as it is

Is this a more accurate way of comparing DateTime? Not in the slightest. In fact, that is how the > operator is implemented internally. From the .NET Reference source: public static bool operator >(DateTime t1, DateTime t2) { return t1.InternalTicks > t2.InternalTicks; } Someone might have thought they were being clever by skipping the one … Read more

Copy files to clipboard in C#

Consider using the Clipboard class. It features all the methods necessary for putting data on the Windows clipboard and to retrieve data from the Windows clipboard. StringCollection paths = new StringCollection(); paths.Add(“f:\\temp\\test.txt”); paths.Add(“f:\\temp\\test2.txt”); Clipboard.SetFileDropList(paths); The code above will put the files test.txt and test2.txt for copy on the Windows Clipboard. After executing the code you … Read more

Map string column in Entity Framework to Enum

Probably a nicer version. OrderStateIdentifier field is used for both JSON serialization and database field, while OrderState is only used in the code for convenience. public string OrderStateIdentifier { get { return OrderState.ToString(); } set { OrderState = value.ToEnum<OrderState>(); } } [NotMapped] [JsonIgnore] public OrderState OrderState { get; set; } public static class EnumHelper { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)