Wednesday, 5 October 2016

C# SQL linked database



SQL architecture


//domain

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace domain
{
    public class book
    {
        public int id { get; set; }

        public string title { get; set; }

        public string isbn { get; set; }

        //for SQL secondary key, format has to be xxid, or won't be recogonized as key
        public int Aid { get; set; }      

        public author A { get; set; }
   
    }
}

-----------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace domain
{
    public class author
    {
        public int id { get; set; }

        public string first_name { get; set; }

        public string last_name { get; set; }

        public IList<book> B { get; set; }
    }
}

---------------------------------------------------
//data
//add Entityframework & domain to reference

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using domain;
using System.Data.Entity;

namespace data
{
    public class data_base:DbContext
    {
        public data_base():base("name = Connection"){ }

        public DbSet<author> As { get; set; }

        public DbSet<book> Bs { get; set; }
    }
}

----------------------------------------------------
//app.config under data

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<connectionStrings>
   <add connectionString="server=DESKTOP-EUFGQAP;database=XYZ;Trusted_Connection=Yes"
           name="Connection"
           providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

------------------------------------------------------
//business
//add data & domain & entityframework to reference

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace business
{
    public class book_package
    {
        public int id { get; set; }

        public string title { get; set; }

        public int Aid { get; set; }

        public string author_name { get; set; }
    }
}

--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using data;
using domain;


namespace business
{
    public class manager
    {
        public static List<book_package> get_all()
        {
            var db = new data_base();

            var books = db.Bs.Select(x => new book_package
            {
                id = x.id,
                title = x.title,
                
                Aid = x.Aid,
                author_name = x.A.first_name+" "+x.A.last_name
            }).ToList();

            return books;

        }
    }
}

-----------------------------------------------------
//presentation
//add business & entityframework to reference

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using business;

namespace presentation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            dataGridView1.DataSource = manager.get_all();
        }
    }
}

--------------------------------------------------------
//app config under presentation
//copy from app config under data

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add connectionString="server=DESKTOP-EUFGQAP;database=XYZ;Trusted_Connection=Yes" name="Connection" providerName="System.Data.SqlClient" />
  </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

-----------------------------------------------------------------------------------

SQL author database



SQL book database


visual studio tablegrid view


-----------------------------------------------------------------
//migration
//use when database table design is changed

//view-> other window -> package manage console

//Enable-Migrations

//Add-Migration Add-Migration AddbooksAid //(format: ADD+database+changed property)

//Update-Database




No comments:

Post a Comment