How to use EntityFramework CodeFirst to update your database
Tech-Today

How to use EntityFramework CodeFirst to update your database


This tutorial will attempt to explain how to update an already existing database using EntityFramework's CodeFirst approach.
We need:
1.) VS2010 SP1
2.) NuGet
3.) EntityFramework 4.1
4.) EntityFramework.Migrations
  Steps: 1.) We need to make sure that we have Package Manager Console installed in VS2010 (Open the view).
  2.) In Package Manager Console execute:
    a.) Install-Package EntityFramework
    b.) Install-Package EntityFramework.Migrations

Delete all tables. Update-Database –TargetMigration:"0"
Update-Database Add-Migration InitializeWeddingDb

How to create a one-to-one relationship V1:
    [Table("WeddingProducts")]
public class WeddingProduct : BaseEntity
{
public long WeddingMenuId { get; set; }
public WeddingMenu WeddingMenu { get; set; }
}

[Table("WeddingMenus")]
public class WeddingMenu : BaseEntity
{
}

//On Context Class override OnModelCreating()
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
modelBuilder.Entity().HasRequired(p => p.WeddingMenu).WithMany().HasForeignKey(p => p.WeddingMenuId);
base.OnModelCreating(modelBuilder);
}
V2 - This one has Cascade: Delete set to true by default
    [Table("WeddingProducts")]
public class WeddingProduct : BaseEntity
{
public long WeddingMenuId { get; set; }
[ForeignKey("WeddingMenuId")]
public WeddingMenu WeddingMenu { get; set; }
}

[Table("WeddingMenus")]
public class WeddingMenu : BaseEntity
{
}

//On Context Class override OnModelCreating()
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
//modelBuilder.Entity().HasRequired(p => p.WeddingMenu).WithMany().HasForeignKey(p => p.WeddingMenuId);
base.OnModelCreating(modelBuilder);
}
How to call stored procedure in codefirst (can be achive using the DbContext class):
IList weddingMenuList = _context.Database.SqlQuery("spGetWeddingMenu").ToList();


To call with parameter
SqlParameter param = new SqlParameter("@productId", productId);
IList weddingMenuList = _context.Database.SqlQuery("spGetWeddingMenu @productId", param).FirstOrDefault();
Note that model-first and code-first should not be use on a single project. Since model-first, is the first to come out in the market I assume most of us use this first then want to try code-first. You can create a class library for this. So they are on different assemblies.

Reference: http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-released.aspx




- Java Generics And Multiple Parameterized Class With Inheritance
This tutorial will try to explain how to use multiple generic parameters in Java and also along the way the advantage of using this feature. Normally I use this when I have a method in a base class that returns a generic type. For example: public class...

- Many To Many Relationship In Entity Framework
An example of many-to-many relationship is asp's membership tables, between aspnet_Users and aspnet_Roles. Usually we declare it like this: public class AspUser { public Guid UserId { get; set; } ... public ICollection AspRoles { get; set; } }...

- Mvc3 Dynamic Search Paging Using Pagedlist
Basically there's a great tutorial from this site: Unboxed solutions. -I've copied the codes and add some comments. I'll just try to add my own comments. 1.) First you need to install PagedList.Mvc from Nuget. a.) Go to Tools->Library Package...

- Implement A Stored Procedure In Entity Framework
Implement a stored procedure in Entity Framework 1.) To do this we have to create a database (name according to your preference) and add the following tables: CREATE TABLE [dbo].[Authors]( [AuthorID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](100)...

- Android Bundle Sqlite Database On Application. Create Sqlite Database On First Application Invoke.
How to setup a default sqlite database structure with data in an android application. Add this class and initialize it in your main class that extends Activity. import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;...



Tech-Today








.