add

Saturday, February 24, 2024

How to generate table Model class from SQL Server using EntityFramework Core

You may get into a situation where you have an existing SQL Server, and you want to build a code first DB approach using Entity Framework core.


You can use Scaffold-DbContext to create a model based on the existing database. 

Below are the parameters that can be specified with Scaffold-DbContext in Package Manager Console:

Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>] 
                    [-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>]

In Visual Studio, select menu Tools --> NuGet Package Manger --> Package Manger Console and run the following command:

Scaffold-DbContext "Server=.\SQLExpress;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

If you get an error like : 

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.

add attribute : TrustServerCertificate=True to the command. So your final command will look like:

Scaffold-DbContext "Server=servername\instaneName;Database=dbName;user id=userID;password=passwordValue;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models


Saturday, September 16, 2023

ERROR : Only the invariant culture is supported in globalization-invariant mode.

 Hi All,


While working with dot net core, if you encounter the following error:

Only the invariant culture is supported in globalization-invariant mode. 

See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')

en-us is an invalid culture identifier.


Here is the simple solution to resolve this:

1. Right click the dot net core project --> Select Edit Project File

2. In the XML file, file the node : 

    <InvariantGlobalization>true</InvariantGlobalization>

3. Replace 'true' with 'false'

The line should be :

    <InvariantGlobalization>false</InvariantGlobalization>

4. Save the project file.

5. Build the project and check for error.

There will be no error on the globalization.

Cheers :) 

Friday, September 15, 2023

.NET 8 preview not showing in Visual Studio 2022

 Have you faced an issue where .Net 8 version framework is not available to select while creating a new .Net core project?


Steps to check:

  1. Check whether you have installed the latest preview of Visual Studio 2022 (17.7).
  2. Enable Preview feature usage:
    1. Visual Studio --> Go to 'Tools' --> 'Manage Preview Features'
    2. Ensure you have 'Preview Feature' selected.
    3. Scroll down and on the right you will see an option 'Use Preview of the .Net SDK'
    4. Select the option. Click 'Ok' and restart the Visual Studio
    5. Now you will have the .Net 8 Preview SDK listed.









Thursday, September 14, 2023

A connection was successfully established with the server, but then an error occurred during the login process.

 If you are creating the database using code first db approach using Entityframework Core and while creating the database using 'Update Database' command encounter the below error:


A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

Chances are there that there is an issue with the connection string mentioned in the web.config or appsettings.json (if using asp.net core app).

Compare the below before and after connection string :

OLD:

data source=YOUR_SERVER_NAME;initial catalog=YOUR_DB_NAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework


NEW:

data source=YOUR_SERVER_NAME;initial catalog=YOUR_DB_NAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;Trusted_Connection=SSPI;Encrypt=false;


Add the above highlighted part at the end of the connection string and try to build the database.

It should work.

Cheers :) 

Tuesday, November 1, 2011

Convert DATETIME TO TIMESPAN IN C#

In the previous post, I mentioned how to convert DATETIME to TIMESPAN. Now if the requirement is to convert DATETIME TO TIMESPAN, we can do that in one line: DateTime dt="XXX"; TimeSpan TS = new TimeSpan(dt.Now.Ticks); This will convert the DateTime to TimeSPan. Varun

Convert TimeSpan To DateTime in c# + One line Code

We usually fall into the situation where conversion of Timespan to DateTime is required.

Here is how it can be done easily just by casting:

Suppose we have two variables DateTime and Timespan and Timespan one with some value
DateTime dt;
TimeSpan ts="XXX";

We can covnert 'ts' to 'dt' like this:

dt= Convert.ToDateTime(ts.ToString());

Thats it!!!

Hope you have liked the solution.

Varun

Tuesday, February 2, 2010

Microsoft Access was unable to create the .accde, .mde or .ade file

Problem

Many times we have faced issue that after creating the MDB file, when we try to compile the database to MDE file ,Access gives the following error :

Microsoft Access was unable to create the .accde, .mde or .ade file

On clicking the button 'Show Help', it shows following error message:

This error is usually associated with compiling a large database into an MDE file.  Because of the method used to compile the database, a considerable number of TableID references are created for each table.  The Access database engine can only create a maximum of 2048 open TableIDs at one time.  Exporting a database as an MDE potentially can exceed this limit if the database has a large number of objects (table, macro, form, report, etc).
There is no accurate method to estimate the number of TableIDs the Access database engine uses during the process of compiling a database as an MDE.  However, each VBA module and each form uses one TableID, as a result, if the database has 500 forms, and each form's HasModule property is set to Yes, as many as 1,000 TableIDs are used.


Solution:

The main reason of database not getting compiled to mde is that it failed during compilation. That is , yiu have some code tha failed to compile.

Steps:

1. Press Alt + F11 to display VBA editor.
2. In the menu 'Debug' , you will find 1st menu item as 'Compile AccessDb'.
3. Click that option.
4. If there is no error, the database will get compiled.
5. If during compilation, any error is encountered, the VBA editor will display that error and you can rectify that.
6. Once all errors get rectified, you will be able to compile the database.


Once successfully compiled, try making the mde again ans it will definitely make the mde file.