Category Archives: Tips and Tricks

Defining an Unique Key for a property of a class in Entity Framework CodeFirst Approach

Introduction

This article explain how to Add an Unique Key for a column in a model in Enity Framework code first approch with the help of Data Annotations.

Approach

Lets say you want add Unique Key constraint for a following class string data type First_Name Property

    // Sample Class
    public class Sample
    {

            public int Id { get; set; }

            public string First_Name { get; set; }

            public string Last_Name { get; set; }
    }

Then You need to define that field with Following tags

  1.  Unique key constraint –   [Index(“IX_AnyName”, 1, IsUnique = true)]
  2.  Length of the Column – (Mandatory if this is String data type ) – [StringLength(Length in Int)]

So After above changes Code will look like this

 

   // Sample Class
    public class Sample
    {

            public int Id { get; set; }

            [Index("IX_FirstNameUniqueKey", 1, IsUnique = true)]
            [StringLength(50)]
            public string First_Name { get; set; }

            public string Last_Name { get; set; }
    }

So After above changes You will notice compile time errors in code

to avoid those we have to import following namespaces

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

After above changes you will notice another error remaining , like below

That because actually Index Attribute is in the System.ComponentModel.DataAnnotations.Schemanamespace that it was in the System.ComponentMode.DataAnnotations assembly – it’s actually in theEntityFramework assembly, so you need Install Entity Framework for this project.

In this Article I explained How to Install the Entity Framework for the Project in Visual Studio

Defining a Class with Foreign Key in Entity Framework CodeFirst Approach

Introduction

This tip explains how to add a Foreign Key for a column in a model in Entity Framework code first approach with the help of Data Annotations.

Approach

We start with a simple scenario. We know every Book has an Author.

So we build our classes like below:

//Author Class
public class Author
{
       public int AuthorId { get; set; }

       public string AuthorName { get; set; }
}

 

//Book Class
public class Book
{
        public int BookId { get; set; }

        public string BookName { get; set; }

        public int AuthorId { get; set; }
}

And you want to create generate tables that consist of AuthorId foreign key in Book Table.

Like below:

sample9

To achieve the above task, you have to use Data Annotations and the following namespace in your classes:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

So now your code needs to improve as shown below:

//Author Class
public class Author
{
        public Author()
        {

        }

        public int AuthorId { get; set; }

        public string AuthorName { get; set; }

        public ICollection<Book> Books { get; set; }
}
//Book Class
public class Book
{
        public Book()
        {

        }

        public int BookId { get; set; }

        public string BookName { get; set; }

        [ForeignKey("Author")]
        public int AuthorId { get; set; }

        public Author Author { get; set; }
}

Now you can easily generate two tables which are Book and Author with Foreign Key Constraint.

Installing the Entity Framework for the Project in Visual Studio 2015

Introduction

This is a Simple Article to explain how to install Entity Framework for Project in Latest IDE Visual Studio 2015

Steps

1. Right Click on Project

sample3

 

2. Select “Manage Nuget Pakages”

sample5

 

 

please make sure above thing in red rectangle on above images are same with yours , If so

3. Hit “Install”

Then It will come up following popup

sample6

 

4. Hit “OK”

Then Again You will meet following Popup

sample7

5. Hit “I Accept”

After Sucessfully installtion you will notice image like below

sample8

I found out valuable facts about world famous android OS

android-icon

From Rahul

1). It wasn’t Google’s idea: Android was the brainchild of Andy Rubin, who founded Android Inc. in October 2003 with the aim of creating a new mobile platform. Google later bought Android Inc. and hired Rubin and others in August 2005.

Andy Rubin
Andy Rubin

2). It almost didn’t work out: Android almost immediately ran out of cash after its founding, only to be saved, according to the Businessweek, by Steve Perlman.

Steve Perlman

3). The Nexus line was a hot rumour years before the Nexus One: People started predicting about the “gPhone” as early as 2007 though Nexus came out in 2010.

gPhone

4). Microsoft thought it would be a non-event: Microsoft’s Scott Horn, then head of the Windows Mobile marketing team, had told Engadget after Android’s release, “I don’t understand the impact they are going to have.”

Scott Horn
( A Former head of the Windows Mobile marketing team)

5). Resolution scaling was introduced in Version 1.6: The ability to automatically scale images based on display size appeared in Donut, or Android 1.6, paving the way for the huge range of device form factors on the Android market today.

Donut – Android 1.6

6). There’s an Android phone in space: A British firm launched a Nexus phone on Space, to control a satellite as part of an experiment and see how well consumer-grade electronics stand up to the rigors of space.

NASA sends another cheap Android-powered satellite in space, makes it dial home

 

7). Every app you run on your Android phone gets its own virtual machine: Each active app on an Android device runs in its own Dalvik VM, which keeps it safe and separate from core functions. This improves battery life and boosts performance of the phone.

The Android OS stack and Dalvik VM

 

The first official version code name was NOT a dessert: Google’s Dan Morrill confirmed in January that the very first alpha version of Android released to internal developers was R2-D2.

r2-d2

================================================

Simple PHP Code show, How many Days Remain to Holiday

<?php 
$target = mktime(0, 0, 0, 12, 25, 2014);
 //within above bracket you need give (0, 0, 0, Month, Date, Year )
$today = time() ;
$difference =($target-$today) ;
$days =(int) ($difference/86400) ;
print "Holiday is in $days days";
?>

This PHP code automatically count and illustrate how many Days remain to your holiday

click this link to view online how this code works http://codepad.org/Je3RTysF