EF DB First -> Code First

We can auto generate Code First POCO Classes using Add Model and Code First From Existing Database Option.

When we use above option we we will get our POCO classes but we may encounter few issues with them so we need to take a look back to them again for following things

  • Once model are generated
    • Identity Column
    • Primary Key
    • Relations
  • Every time migration is added
    • “dbo.” Should be replaced with “”
    • pk name and fk names should be manually adjusted as all key names in existing mssql db are name in covention like “PK_tablename” not -> “PK_dbo.tablename”
    • Same things we need to take care for Foreign Keys also
  • For Each Computed Columns Values must be re-defined using sql manually

Snippet

DropColumn("usd01usd_booking", "usd01balance_amt");
Sql("Alter Table usd01usd_booking add usd01balance_amt as (((([usd01opening_amt]+[usd01amount])-[usd01settled_amt])-[usd01reversed_amt])+[usd01rev_settled_amt]) PERSISTED");
  • For each views and stored procedures SQL Commands must be run
  • Watchout for Schema Name that will be used while creating new tables
  • Pain in ass when you forget to add default values
    • In case of new column to existing table with data
    • Also don’t miss the relation/foreign key part