About Hasmukh patel

My Photo
Harrow, London, United Kingdom
Dot-Net developer with expertise in Web, WPF, Win-form applications. Have worked on Asp.net,mvc , WPF and Win-forms projects in c#.net language having Sql-Server/Oracle as database with service oriented architecture using test driven development. Having complete knowledge of SDLC and have successfully worked and implemented it on projects.

Generate EF code first DBContext from Database

Generate EF code first DBContext from existing Database
SET NOCOUNT off 
begin
 declare @UserTables table
    (
            Id [int],
            Name [nvarchar](200),
            PropertyName [nvarchar](200)
    )
 
 insert into @UserTables
  select id, name ,
   case 
   when  Right(Name,2) in ('ss','ch') then Name + 'es'
   when  Right(Name,2) = 'ff' then left(Name, len(Name)-2) + 'ves'
   when  Right(Name,2) = 'fe' then left(Name, len(Name)-2) + 'ves'
   when  Right(Name,1) = 'f' then left(Name, len(Name)-1) + 'ves'
   when  Right(Name,1) = 'y' then 
     case when IsPrecedingVowel = 1 
     then Name + 's' 
     else left(Name, len(Name)-1) + 'ies'
     end
   when  Right(Name,1) = 'o' then
     case when IsPrecedingVowel = 1 
     then Name + 's' 
     else Name + 'es'
     end
   else Name + 's' 
   end as PropertyName
  from ( select id, name,
   case when left(right(name,2),1) in ('a','e','i','o','u') then 1 else 0 end as IsPrecedingVowel
    from sysobjects where xtype = 'U' ) as x
 
    --select * from INFORMATION_SCHEMA.TABLES 
 
 
 declare @EntityPriFix [nvarchar](20);
 
 declare @Id int;         
 declare @TableName [nvarchar](200);
 declare @PropertyName [nvarchar](200);
  
 set @EntityPriFix = 'POCO';
 
 declare tableCursor cursor for
  select * from @UserTables
 
 print 'using System.Data.Entity;'
 
 print ' public class ' + db_name() + 'Context : DbContext'
 print ' {'
 print '  public QslLoyaltyContext(string connectionString)'
 print '   : base(connectionString)'
 print '        { }'
 
 OPEN tableCursor
 
 FETCH NEXT FROM tableCursor 
  INTO @Id, @TableName, @PropertyName
 
 WHILE @@FETCH_STATUS = 0
 BEGIN
  
  print '  public DbSet<'+ @EntityPriFix + @TableName +'> ' + @PropertyName +' { get; set; }'
  
 FETCH NEXT FROM tableCursor 
  INTO @Id, @TableName, @PropertyName
 END 
 CLOSE tableCursor;
 
 print '  protected override void OnModelCreating(DbModelBuilder modelBuilder)'
 print '  {'
 OPEN tableCursor
 
 FETCH NEXT FROM tableCursor 
  INTO @Id, @TableName, @PropertyName
 
 WHILE @@FETCH_STATUS = 0
 BEGIN
  
  print '   modelBuilder.Entity<'+ @EntityPriFix + @TableName +'>().ToTable("' + @TableName +'");'
  
 FETCH NEXT FROM tableCursor 
  INTO @Id, @TableName, @PropertyName
 END
 print '  }'
 
 CLOSE tableCursor;
 DEALLOCATE tableCursor; 
 
 print ' }'
 print '' 
end
 
Output will be like as below
using System.Data.Entity;
public class QslLoyaltyContext : DbContext
{
    public QslLoyaltyContext(string connectionString)
        : base(connectionString)
    { }
    
    public DbSet<POCOUsers> Userss { getset; }
    public DbSet<POCORoles> Roless { getset; }
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<POCOUsers>().ToTable("Users");
        modelBuilder.Entity<POCORoles>().ToTable("Roles");
    }
}
 

No comments :