Skip to main content
added File.Copy variant
Source Link
OctoCode
  • 390
  • 4
  • 14
  1. First, create the new migration file (or use the existing one).
  2. Inside Up() method you can put code for file upload, and inside Down() method code for file removal from repo (in case you want to revert migration).

Below is one of the many possible ways to do some remote upload, this is one of the simplest:

using (var webClient = new WebClient()) { webClient.UploadFile("ftp://localhost/samplefile.jpg", "samplefile.jpg"); } 

For this to work you should add using System.Net; to the migration file. Also, obviously you need to handle upload permissions and credentials, depending on the type of remote repo you are using.

After this you just need to run Update-DatabaseEDIT:

Using File object is even more trivial. Here is the complete code for migration class:

using System; using System.Data.Entity.Migrations; using System.IO; public partial class MigrationWithFileCopy : DbMigration { public override void Up() { File.Copy("sourceFile.jpg", "destinationFile.jpg"); } public override void Down() { File.Delete("destinationFile.jpg"); } } 
  1. First, create the new migration file (or use the existing one).
  2. Inside Up() method you can put code for file upload, and inside Down() method code for file removal from repo (in case you want to revert migration).

Below is one of the many possible ways to do some remote upload, this is one of the simplest:

using (var webClient = new WebClient()) { webClient.UploadFile("ftp://localhost/samplefile.jpg", "samplefile.jpg"); } 

For this to work you should add using System.Net; to the migration file. Also, obviously you need to handle upload permissions and credentials, depending on the type of remote repo you are using.

After this you just need to run Update-Database.

  1. First, create the new migration file (or use the existing one).
  2. Inside Up() method you can put code for file upload, and inside Down() method code for file removal from repo (in case you want to revert migration).

Below is one of the many possible ways to do some remote upload, this is one of the simplest:

using (var webClient = new WebClient()) { webClient.UploadFile("ftp://localhost/samplefile.jpg", "samplefile.jpg"); } 

For this to work you should add using System.Net; to the migration file. Also, obviously you need to handle upload permissions and credentials, depending on the type of remote repo you are using.

EDIT:

Using File object is even more trivial. Here is the complete code for migration class:

using System; using System.Data.Entity.Migrations; using System.IO; public partial class MigrationWithFileCopy : DbMigration { public override void Up() { File.Copy("sourceFile.jpg", "destinationFile.jpg"); } public override void Down() { File.Delete("destinationFile.jpg"); } } 
Source Link
OctoCode
  • 390
  • 4
  • 14

  1. First, create the new migration file (or use the existing one).
  2. Inside Up() method you can put code for file upload, and inside Down() method code for file removal from repo (in case you want to revert migration).

Below is one of the many possible ways to do some remote upload, this is one of the simplest:

using (var webClient = new WebClient()) { webClient.UploadFile("ftp://localhost/samplefile.jpg", "samplefile.jpg"); } 

For this to work you should add using System.Net; to the migration file. Also, obviously you need to handle upload permissions and credentials, depending on the type of remote repo you are using.

After this you just need to run Update-Database.