Friday, September 6, 2013

Creating a new database backed ASP.NET MVC Application using Entity Framework

This post is more of a reminder to myself on how to setup and pass data through a simple MVC app.


Creating a new data driven MVC Application (MyAppMVC) using Entity Framework

Add projects
MyAppMVC.Data.Model for the data model and MyAppMVC.Data for the DataContext (with dbset<> ) and migrations folder (include configuration.cs and then add a Seeder class)

Add an Adapters Folder with subfolders Data, Interface and Mock to the main (startup) project.

Add References between the projects:
MyAppMVC references MyAppMVC.Data and MyAppMVC.Data.Model
MyAppMVC.Data references MyAppMVC.Data.Model

Unit test References if needed

Add the Entity Framework Package (NuGet)

Add Data Model classes to .Data.Model

Add ------Context.cs class to .Data (it is a :DbContext)
in this class add DbSets (public DbSet<----> -------s { get; set; }

In Package Manager enable-migrations under .Data --- a Migrations folder with
Configuration.cs file is added

Seed data via Configuration.cs
Add a Seeder.Seed(context) to Configuration and create a Seeder file with a Seed method that takes the dbcontext as a parameter. using System.Data.Entity.Migrations (for AddOrUpdate extension method)

public static void Seed(-----Context context)
        {
            context.------.AddOrUpdate(c => c.----, new ---- { Name="James" });
        }

add-migration initial and then update-database. Open SQL Server Management Studio or VS Database Explorer and celebrate.  VS use (LocalDb)\v11.0

To move data up the stack we use an adapter class to fetch data from the database and return a view model. 

Create a new cotroller (Home) and right click then add a view(Index).

Next add a view model that contains the data the view needs. (Right click Model (top level) and add a .cs class called -------ViewModel ).  Add the data needed from using .Data.Model project .

Right click Interface in Adapter and add an I----Adapter interface that has a method Get------ViewModel that returns the desired view model.  Implement the interface in the Data (and Mock) folders.
 public class ----Adapter : -----.Adapter.Interface.I----Adapter
    {
        public Models.------ViewModel Get------ViewModel()
        {
// create the viewmodel and dbcontext to work with
            ----Context db = new ----Context();
            -----ViewModel model = new -----ViewModel();
            model.----- = db.-----.ToList();    //.FirstOrDefault() for 1 item
// filter with .Where and use .take(#) .orderby() ......
            return model;      
        }
    }
// the adapter queries the database and returns our view model

Now we utilize the adapter in the Controller Action and return the view model to the view.
 -----Adapter _adapter;
        public HomeController()
        {
            _adapter = new -----Adapter();
        }

        public ActionResult Index()
        {
            return View(_adapter.Get------ViewModel());
        }

In the view we specify a view model
@model -----MVC.Models.-----ViewModel        
to loop through a List item with razor
@foreach(var element in Model.-----)
{
@element.-----
}

We have now built a database using code first and the Entity Framework. We then queried that database and passed the results up to a view model.

Monday, August 26, 2013

Week Two Begins

Today we worked with SQL Server Management Studio and looked at working with a database at a somewhat lower level than we will be using from here on out.  We created some new tables and performed some of the basic CRUD functions.

Create
INSERT INTO <Table>(ID, Name, EmployeeNum)  VALUES (1, 'James', 23) 

Read
SELECT * FROM <Table>
WHERE Name='James'

SELECT * From <Table1> AS t1
JOIN <Table2> AS t2 ON t1.<field> = t2.<field>  

Update
UPDATE <Table> 
SET Name = 'Jamie' 
WHERE ID = 1

Delete
DELETE FROM <Table>
WHERE ID = 1

The JOIN above is what is known as an inner join and seems to be the most common type of join.  If the fields in the tables match then that row is included in the result set. Left Outer Join and Right Outer Join can also be used to join tables (among a few other ways). In say a left outer join the result set is similar to a normal inner join except that if no match is found in the second table then the row from the first table is still included in the results (every row from the left table is in the result set).  

Since we are using the code first approach we will not be spending much time on learning SQL. We also touched on transforming our data model classes from the code first approach into a database. And using Visual Studio's Package Manager Console to perform commands such as Enable-migrations, Add-migration, and update-database.  




Monday, August 19, 2013

Coder Camps Day 1

I've finished my first day at Coder Camps. It was fun meeting everyone in the troop and getting started on the path to becoming a jr web developer.  Our troop will be working on a crowd-funding web site over the next nine weeks. So, we started thinking about that and also touched on a few other things.  One being an overview of OOP concepts.  These included inheritance, polymorphism and encapsulation (data hiding). I'm looking forward to another great day tomorrow.      

Monday, July 29, 2013

My Home Page

I've tied my Tic Tac Toe, Carnival Cruise, and Troop #29 pages together and made myself a personal home page.  Nothing extravagant here but kind of cool to have a web presence nonetheless.
As you might have guessed by now it can be found at www.jdsv650.somee.com .

Wednesday, July 24, 2013

Carnival Cruise (And of course an attempt at a web page)

It looks like I'll be heading to Texas a bit on the early side.  I've booked my first ever cruise on Carnival's /in/famous Triumph. My original intent was to use this as an opportunity to kick back and do a good bit of studying to prepare for <CoderCamps />. Since the original booking, my 12 year old and ex- father in law have decided to join me and make it an all out vacation.  Check out our itinerary here http://www.jdsv650.somee.com/Cruise/cruise.html   I'm counting down the days until I'm in Galveston (and of course Pearland).
       

Saturday, July 13, 2013

My Tic Tac Toe Native Mobile App - Web App

I managed to upload a simple Tic Tac Toe game to the three main app stores a few months back  (or maybe I should say two main app stores plus Windows Phone Store) .  Here is a look at the download results to date (7/13/13).

App Store (iOS)


Windows Phone Store


Google Play Store


As you can see the results range from 2 downloads on Android (one of them being me :0) to thousands of downloads on the App store.  While admittedly not the greatest game/app ever, it is still exciting to see that many people use an app that you have created!!!  Search for "James Donner" or "TicXOToe" on any of these stores and check it out if you are so inclined.

In keeping with the Tic Toe Toe tradition, I've now ported the game to JavaScript and found a free web hosting site http://www.jdsv650.somee.com/ticxotoe.html called Somee.  The source code can be found on my GitHub @ https://github.com/jdsv650.

Monday, June 24, 2013

Basic HTML Tags and Troop #29 Intro

<!DOCTYPE html>
<html>
    <head>
   
        <title> Troop #29   </title>
        <!-- link to external css stylesheet -->
        <link type="text/css" rel="stylesheet" href="stylesheet.css">
        <!-- external JavaScript file
        <script type="text/javascript" src="myJS.js"> </script>  -->
       
    </head>
    <body>
   
        <div id="CChead">    <!-- class="myClass"  then in css .myClass -->
            <!-- h1, h2, h3 ... h6 -->
            <h1> &lt; <span style="color:blue;"> Coder </span> Camps /&gt; </h1>
        </div>
       
        <h2>  Meet the campers of troop #29  </h2>
       
        <p>  
              A group of individuals with various backgrounds, uniting from all over the globe (or at least the United States), 
              <br/>
              with the common goal of learning software development/web site development and redefining our professional lives.
        </p>
       
        <table border="2">
                   
            <tr>
                <td>
                    <h4> Yinjie Luo  </h4>
                    <a href="http://yinjiel.blogspot.com/">
                        <img src="images/yinjieluo.jpg">
                    </a>
                </td>           
       
                <td>
                    <h4> Lacey Mulkey  </h4>
                    <a href="">
                        <img src="images/laceymulkey.jpg">
                    </a>
                </td>       
       
                <td>
                    <h4> Isaac Williams  </h4>
                    <a href="https://plus.google.com/118245179669731297062/posts">
                        <img src="images/isaacwilliams.jpg">
                    </a>
                </td>   
            </tr>
           
            <tr>
                <td>
                    <h4> Ryan McDaniel </h4>
                    <a href="">
                        <img src="images/ryanmcdaniel.jpg">
                    </a>
                </td>   
       
                <td>
                    <h4> Shawn Saeed  </h4>
                    <a href="http://shawnsaeed.blogspot.com/">
                        <img src="images/shawnsaeed.jpg">
                    </a>
                </td>   
       
                <td>
                    <h4> Debra Joyner  </h4>
                    <a href="http://debrajoyner.blogspot.com/">
                        <img src="images/debrajoyner.jpeg">
                    </a>
                </td>   
            </tr>
           
            <tr>
                <td>
                    <h4> Yola Lu </h4>
                    <a href="http://yolajlucc29.blogspot.com/">
                        <img src="images/yolalu.jpg">
                    </a>
                </td>   
       
                <td>
                    <h4> Daniel Marston </h4>
                    <a href="">
                        <img src="images/danielmarston.jpg">
                    </a>
                </td>   
       
                <td>
                    <h4> Andy Trewyn </h4>
                    <a href="http://atrewyn.blogspot.com/">
                        <img src="images/andytrewyn.jpg">
                    </a>
                </td>
            </tr>       
       
            <tr>
                <td>
                    <h4> J. Andrew Brassington </h4>
                    <a href="http://bootcampcoder.blogspot.com/">
                        <img src="images/andrewbrassington.png">
                    </a>
                </td>
               
                <td>
                    <h4> James Donner </h4>
                    <a href="http://nibblebucket.blogspot.com/">
                        <img src="images/jamesdonner.jpg">
                    </a>
                </td>
       
                <td>
                    <ul>  <!-- unordered (bulleted) list or use <ol> for ordered (numbered> list  -->
                        <li> <h4> Boris Shporkin  </h4> </li>
       
                        <li> <h4> James Graham </h4> </li>
       
                        <li> <h4> Amijot Sohal  </h4> </li>
                    </ul>
               
                </td>
            </tr>   
        </table>

    </body>
</html>