Cybercrux

Everything is achievable through technology

Entity framework code first

Enable-Migrations

add-migration {migration name} eg:add-migration TableAdded

Update-Database

Update-Database -Script – to generate the sql script

Update-Database -Script
-SourceMigration:”201502201618119_Migrations17″
-TargetMigration:”201503031134340_Migrations18″

Update-Database -TargetMigration $InitialDatabase

To get the entire script: Update-Database -Script -SourceMigration:0

rollback to a migration: Update-Database -TargetMigration:”name_of_migration”

 

 

Error Check: (System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors.First().ValidationErrors.First()

validation

catch (DbEntityValidationException ex)
{
foreach (var validationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format(“{0}:{1}”,
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
_logger.Trace()
.Message(string.Format(“error: {0} & Error : {1}”, validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage))
.Write();
}
}throw ex;
}

 

Interceptor

<entityFramework>
<interceptors>
<interceptor type=”System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework”>
<parameters>
<parameter value=”C:\LogOutput.txt”/>
</parameters>
</interceptor>
</interceptors>
</entityFramework>