Wednesday, 6 May 2020

How To connect MySQL with Entity Framework Core

This article explains how to connect MySQL using Entity Framework Core. 

This one require Visual Studio 2017, Dot net Core 2.0 and above and MySQL Version 8.0.12 and above.

I know you have a question how to find MySQL version and the answer is below.

  • Open MySQL Workbench
  • Run this query select @@version
Okay Lets Start.   
1. Select File -->New --> Project in the Visual Studio
2. Select 'Console Application (.NET Core)'  under '.Net Core' in the New Project Window

3. Solution explore will looks like below
4. Add New Project 
5. Select 'Class Library(.Net Core);

6. Open NuGet Packet Manager for Data Layer project.

7. It is displaying all installed packages

8. Install below packages
  • MySql.Data.EntityFrameworkCore --> Version 6.10.9
  • MySql.Data.EntityFrameworkCore.Design -->  Version 6.10.9
  • Microsoft.EntityFrameworkCroe.Tools --> version 2.0.3
Installed Packages.

9.  Create new database in MySQL.  https://www.mysqltutorial.org/mysql-sample-database.aspx/ use this if you really need. 
10. Scaffold DB models using Package Manager Console.
  • Open Package Manage Console and Select Default Project to your EF Core Project 
  • Run below command with your own values.
Scaffold-DbContext  "server=localhost;port=3306;user=root;password=password;database=classicmodels" MySql.Data.EntityFrameworkCore -outputdir Models/ClassicModels -f

  
Scaffold result folders as below


11.  Add DataLayer as Dependency of "MyApplication"


12.  Use below code in the Program.cs


using System;
using DataLayer.Models.ClassicModels;
using System.Linq;
using System.Collections.Generic;


namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (classicmodelsContext context = new classicmodelsContext())
            {
                List<string> cityCollection = context.Offices.Select(office => office.City).ToList();
                Console.WriteLine("Office list as below");
                foreach(string city in cityCollection)
                {
                    Console.WriteLine(city);
                }

            }

            Console.ReadKey();

        }
    }
}


13.  Output as below


Happy Coding :)

Wednesday, 6 May 2015

How to resolve .ascx.g.cs missing in SharePoint 2013


Normally this issue happened in the Visual Webpart (refer below screen shot).


The structure of this webpart is look likes below.


If you missed the site url in the project property or VS failed to communicate SP then the g.cs file is removed from the project.   This will not give any error or warning message but your updates in the ascx will not reflect in the ascx.cs file and webpart.

Steps to Resolve

                Below steps for the people who are not using any SVN because in SVN we can restore to the previous version.

1.       Add new item and select text file Name it as CalculatorWebpart.ascx.g.cs




        2.    Move the file inside “CalculatorWebpart”.
      3.   Set valid site URL in the project property by select the property then hit F4 key



         4.  Create a dummy visual webpart to get sample entries in the project file.  I named it as “Dummy          Webpart”.  If you have another properly working webpart then skip this step.
        5.  "Save All” the project file
        6. Open project in the File Explorer.
        7.  Move the “CalculatorWebpart.ascx.g.cs” to the “CalculatorWebpart” (webpart) folder.
        8.  Right click the project file(.proj) and open in the “Notepad”.
        9.  Find “DummyWebpart.ascx.g.cs” you have two entries make similar entries for
           “CalculatorWebpart.ascx.g.cs” in the same area.


    <Compile Include="CalculatorWebpart\CalculatorWebpart.ascx.g.cs" />
Become
<Compile Include="CalculatorWebpart\CalculatorWebpart.ascx.g.cs" >
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>CalculatorWebpart.ascx</DependentUpon>
    </Compile>

And
                <Content Include="CalculatorWebpart\CalculatorWebpart.ascx">
      <Generator>SharePointWebPartCodeGenerator</Generator>
</Content>

Become
                    <Content Include="CalculatorWebpart\CalculatorWebpart.ascx">
<Generator>SharePointWebPartCodeGenerator</Generator>
                                <LastGenOutput>CalculatorWebpart.ascx.g.cs</LastGenOutput>
    </Content>

    10.   Save the .proj file.
    11.   Go the VS and refresh the project.
    12.   Open “CalculatorWebpart.ascx” do any modification and retract it.
    13.   Now you can see the updated “CalculatorWebpart.ascx.g.cs”

Happy Coding :)
-
Sugunthan Shanmuga Sundaram.