none
EF 4.3 + Code First Migrations + FileStream How? RRS feed

  • Question

  • Hi All.

    I'm trying to figure out (my database already has file streaming enabled) how to update the code first migration script so that I can make the column creation for a specific column create a filestream column. I don't need the streaming function in my actual model, but I do need it to store it in a filestream because it's so much better for fragmentation etc.

    Any ideas on how to update CreateTable or AddColumn call to add the right filestream stuff?

    thanks!

    Friday, February 24, 2012 2:13 PM

Answers

  • Hi JohnGalt,

    Based on my testing, I don't think there is a good way to handle FileStream Type in Migration now. Here is my testing code:

    public class Record { public System.Guid Id { get; set; } public Nullable<int> SerialNumber { get; set; } public byte[] Chart { get; set; } public byte[] Chart1 { get; set; } // migration add this Chart1 as FileStream in my FileStream enable database } //My Migrations File public partial class test1 : DbMigration { public override void Up() { //AddColumn("Records", "Chart1", c => c.Binary(nullable: false));

      Sql("alter table Records add  Chart1 varbinary(max) FILESTREAM NULL"); // add new Filestream column here } public override void Down() { DropColumn("Records", "Chart1"); } } // My console Main() using (var db= new ArchiveContext()) { Record r = new Record { Id = Guid.NewGuid(), Chart1 = System.IO.File.ReadAllBytes(@"d:\Text.txt"), Chart = System.IO.File.ReadAllBytes(@"d:\Text.txt"), SerialNumber = 12 }; db.Records.Add(r); db.SaveChanges(); // var file = db.Records.First(); //System.IO.File.WriteAllBytes(file.Id + ".txt", file.Chart1); }

    Thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Tuesday, February 28, 2012 6:17 AM
    Moderator
  • I guess it would work to just take out the add column and put in a pure sql command to create the column?
    Tuesday, February 28, 2012 1:31 PM

All replies

  • Hi JohnGalt,

    Welcome!

    We will do some more pending research  about your problem and come back as
    soon as possible, Thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 27, 2012 9:17 AM
    Moderator
  • Hi JohnGalt,

    Based on my testing, I don't think there is a good way to handle FileStream Type in Migration now. Here is my testing code:

    public class Record { public System.Guid Id { get; set; } public Nullable<int> SerialNumber { get; set; } public byte[] Chart { get; set; } public byte[] Chart1 { get; set; } // migration add this Chart1 as FileStream in my FileStream enable database } //My Migrations File public partial class test1 : DbMigration { public override void Up() { //AddColumn("Records", "Chart1", c => c.Binary(nullable: false));

      Sql("alter table Records add  Chart1 varbinary(max) FILESTREAM NULL"); // add new Filestream column here } public override void Down() { DropColumn("Records", "Chart1"); } } // My console Main() using (var db= new ArchiveContext()) { Record r = new Record { Id = Guid.NewGuid(), Chart1 = System.IO.File.ReadAllBytes(@"d:\Text.txt"), Chart = System.IO.File.ReadAllBytes(@"d:\Text.txt"), SerialNumber = 12 }; db.Records.Add(r); db.SaveChanges(); // var file = db.Records.First(); //System.IO.File.WriteAllBytes(file.Id + ".txt", file.Chart1); }

    Thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Tuesday, February 28, 2012 6:17 AM
    Moderator
  • I guess it would work to just take out the add column and put in a pure sql command to create the column?
    Tuesday, February 28, 2012 1:31 PM
  • Hi John,

    You're right.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 29, 2012 3:12 AM
    Moderator
  • any improvements to this workflow? did the migration work?
    Thursday, July 19, 2012 8:07 PM