IT/Software career thread: Invert binary trees for dollars.

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Press F5, u should see a default looking home Page.

Next step we will update your references. Right click on your project and click on "Manage Nuget Refereces" update everything. Afterwards your visual layout will have changed. that is ok.
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Lets do it based on a code first approach for Entity Framework. There are several ways of using EF (Entity Framework). Code first is the most explicit one. It requires more typing but it will give you more control over your stuff.
What is Code-First?

We are also going to use Conventions, meaning the name of certain things matter.
Code-First Conventions in Entity Framework 6
Read those briefly, and once you are ready move to the part that creates a database

Database Initialization in Code-First

Since you already created the DAtabase, you can go ahead and delete it from SMSS. Well recreate it here.

Use the third approach Connection string

Code:
namespace SchoolDataLayer
{
    public class Context: DbContext
    {
        public SchoolDBContext() : base("name=NAMEOFCONNECTIONSTRING")
        {
        }
    }
}

Open your web.config file and add the connection string
Code:
  <connectionStrings>
    <add name="NAMEOFCONNECTIONSTRING"
    connectionString="Data Source=.;Initial Catalog=DATABASENAME;Integrated Security=true"
    providerName="System.Data.SqlClient"/>
    </connectionStrings>
 
  • 1Like
Reactions: 1 user

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Remember that Part where we updated the nuget reference?

Go there again and from the first tab search and install "EntityFramework"
1524494669840.png
 
  • 1Like
Reactions: 1 user

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Im just making changes as i Go. Im not going back and editing.

Create a folder on the root named DataContext
Inside of it create a file named ReportContext, That is your context file

1524494916051.png


ReportContext.cs

Code:
using System.Data.Entity;

namespace MachineReport.DataContext
{
  
    public class ReportContext : DbContext
    {
        public ReportContext() : base("name=MyConnectionString")
        {
        }
    }
}

Add on the web.config a connection string with that name.
 
  • 1Like
Reactions: 1 user

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
On your models Folder

Create these 3 files. t will be the base of all our models

Code:
public abstract class Entity : IObjectState
    {
        #region Public Properties

        [NotMapped]
        public ObjectState ObjectState
        {
            get;
            set;
        }

        #endregion
    }
    public interface IObjectState
    {
        #region Public Properties

        [NotMapped]
        ObjectState ObjectState
        {
            get;
            set;
        }

        #endregion
    }
    public enum ObjectState
    {
        Unchanged,
        Added,
        Modified,
        Deleted
    }

BTW on C# the name of the file is the name of the class, so if i dont specify the file name, its the class/enum/interface inside of it. That last one is actualy 3 separate files.
 
  • 1Like
Reactions: 1 user

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
We have not created any model classes yet.
Lets go ahead and do that. WE are going to create a simple salary class. so you can see the data types
Code:
namespace MachineReport.Models
{
    [Table("Salaries")]
    public class Salary :Entity
    {
        /// <summary>
        /// Main Key, by using conventions, EF will understand that this is an identity column, an ever increasing integer.
        /// </summary>
        public int SalaryId
        {
            get; set;
        }


        /// <summary>
        /// Required Database field with ammount
        /// </summary>
        [Required]
        public decimal Amount
        {
            get; set;
        }

        [MaxLength(300)]
        public string FixedLenth
        {
            get; set;
        }

        public string VeryLongString
        {
            get; set;
        }


        /// <summary>
        /// Required Database fieled with date of effectiveness
        /// </summary>
        [Required]
        public DateTime EffectiveOn
        {
            get; set;
        }

        [Required]
        public bool Inactive
        {
            get; set;
        }

       
        public bool? Deleted
        {
            get; set;
        }
    }
}

I want to draw attention to the [tables("table name")] Sometimes we want to use code names that are separate from the table names.

EF will try to pluralize(sp) the words. So if your class is named Order, the tables will be Orders.
 
  • 1Like
Reactions: 1 user

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
There are also other important tags there. [Required] makes it that you have to have a value there and will mark it on the database as such. Maxlength will make it that your string are not defaulted to the maximum., and the "?" operator. This is a special c# operator meaning "nullable"
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
If anything a separate thread can be used. Ill leave it to amod discretion. But this is really what this thread is for. give me 5 initializing DB is next
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
open the PAckage Manager console. Leave it poen somewhere as you use it a lot on EF. this is how you get to it

1524498107739.png

then type Enable-Migrations

from
Code-based Migration in Entity Framework

1524498405897.png



Modify your context file and lets dd those SAlaries

Code:
public class ReportContext : DbContext
    {
        public ReportContext() : base("name=ReportConnectionString")
        {
            // this forces you to generate the migrations files manually. Its better because you will learn how it affects teh database
            Configuration.AutoDetectChangesEnabled = false;

            // This disables Lazy loading. LAzy laoding its good for simplicity, but bad for database performance.
            Configuration.LazyLoadingEnabled = false;

            // When lazy is on, proxy are created to create the entities. since we dont have laze, we turn it off
            Configuration.ProxyCreationEnabled = false;

            // DAtabase timeout
            Database.CommandTimeout = 300;

        }

        #region Db sets or tables
       
        public DbSet<Salary> Salaries
        {
            get; set;
        }

        #endregion
    }

Then add a migration
Add-Migration InitialCreate


1524498466099.png
 
  • 1Like
Reactions: 1 user