none
Using EntiyFramework.Migrations from Code to Update Database Schema RRS feed

  • Question

  • Hello, I really like the Alpha 3 of EntiyFramework.Migrations.

    Is there (or will be) a possibility to call the upgrade from Code? If I do the following I always tries to create the Database from Scratch. (Doing also the first Upgrade with is already done...) Can I tell the update Function somehow were to start?

            public static void UpdateDatabase() {
                var context = new DbMigrationContext<PersonContext>();
                
                context.MigrationsAssembly = System.Reflection.Assembly.GetAssembly(typeof(PersonContext));
                context.AutomaticMigrationsEnabled = true;
                context.SetCodeGenerator<CSharpMigrationCodeGenerator>();
                context.AddSqlGenerator<SqlConnectionSqlServerMigrationSqlGenerator>();
                context.AutomaticDataLossEnabled = false;
     
                var m = new DbMigrator(context);
                m.Update("MySecondSetOfChanges");
            }
    

     

    Nice greetings from Germany

    Markus

    Wednesday, October 12, 2011 2:10 PM

Answers

  • Hi Markus,

    Welcome!

    To tell truth, Alpha 3 is new to me, I'm not very sure about the workflow's entry, when I tested your code, it works on my computer. I'm not sure about your "from Scratch", do you mean the database was truncated?

    BTW, if you use "No-Magic" Migration, you should set AoutmaticMiggrationsEnabled to false.

    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.

    Thursday, October 13, 2011 7:17 AM
    Moderator

All replies

  • Hi Markus,

    Welcome!

    To tell truth, Alpha 3 is new to me, I'm not very sure about the workflow's entry, when I tested your code, it works on my computer. I'm not sure about your "from Scratch", do you mean the database was truncated?

    BTW, if you use "No-Magic" Migration, you should set AoutmaticMiggrationsEnabled to false.

    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.

    Thursday, October 13, 2011 7:17 AM
    Moderator
  • Hello Alan,

     

    thanks al lot for your quick reply.

    From Scratch: Update wants to run all updates from the beginning and not only the neccessary ones.

     

    But because the code was working for you I started a complete new sample project from the beginning and now my first test was working fine... I don't know what I missed yesterday...???

    I really like Entity Framework and the new 4.1 Update. And the Migration will become really cool and time saving!

     

    Thanks again and have a good day,

    Markus

    Thursday, October 13, 2011 11:04 AM
  • Hi Markus,

    I'm glad to hear you are interested in EF, If you have any questions related to EF, you can post here, we can discuss together!

    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.

    Thursday, October 13, 2011 11:34 AM
    Moderator
  • Hi Chen,

     

    Thanks for your offer! One more question: I want to create a recursive structure. Like Windows Folders... A Folder can contain Child Folders and so on... My class is:

     

    namespace DbLayer.Model {
        public class Folder {
     
            public static Folder CreateNew(string folderName) {
                return new Folder {
                    FolderName = folderName,
                    FolderId = Guid.NewGuid(),
                };
            }
     
            public Guid FolderId { getset; }
            public string FolderName { getset; }
     
            // Recursive Folder Hierachie
            //public Guid ParentFolderId { get; set; }
            // Navigation properties
            public virtual Folder ParentFolder { getset; }
            public virtual ICollection<Folder> ChildFolders { getset; }
        }
    }
    The table generated looks like this:
    FolderId uniqueidentifier Unchecked
    FolderName nvarchar(MAX) Checked
    ParentFolderId uniqueidentifier Unchecked
    ParentFolder_FolderId uniqueidentifier Checked
    If I have the ParentFolderId or not its not making any difference... But this is one Col to much. 
    How can I tell EF to not generate one of the ParentFolder Cols? Should I use an attribute over one 
    of the Navigation Properties?
    And how is the recommended way to initialize the ChildFolders Collection on creation of new Folders?
    Nice greetings from germany,
    Markus
    (It's almost midnight here, so I will probably read your answer tomorrow...)
    Tuesday, October 18, 2011 10:00 PM
  • Hi Markus,

    Greeting! It's morning here. According to your description, you should use Fluent API to change the default mapping:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace Recursive
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var context= new MyContext())
                {
                    context.Database.CreateIfNotExists();
                }
            }
        }
        public class Folder
        {
    
            public static Folder CreateNew(string folderName)
            {
                return new Folder
                {
                    FolderName = folderName,
                    FolderId = Guid.NewGuid(),
                };
            }
    
            public Guid FolderId { get; set; }
            public string FolderName { get; set; }
    
            // Recursive Folder Hierachie
            public Guid ParentFolderId { get; set; }
            // Navigation properties
            public virtual Folder ParentFolder { get; set; }
            public virtual ICollection<Folder> ChildFolders { get; set; }
        }
        public class MyContext:DbContext
        {
            public DbSet<Folder> Folders { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
    
                modelBuilder.Entity<Folder>().HasRequired(f => f.ParentFolder).WithMany(c => c.ChildFolders).HasForeignKey(f => f.ParentFolderId);
            }
        }
    
    }
    
    

    BTW, it is better to open a new thread for a new question.

    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, October 19, 2011 1:18 AM
    Moderator
  • Hello Chen,

     

    thanks a lot for your quick answer. (I will start a new thread with the next question)

    I like the Idea of using Conventions an Annotations more than fluent API.

    The Reason:

    - I can put the Model in an Assembly

    - I can put the Context for the application in another assembly

    - And for Unit Tests I will have a couple of small Contexts using only the part of the model needed for the test.

    So I don't have to create the whole DB for a small unit test... And with code first the database is recreated for me on every run. :-) (I mentioned above already - I really really like the new Code First features in EF 4.1 !!!)

    If I use Fluent API in this scenario I would have to duplicate the model Builder Code in every Context...

     

    I found a simple solution on Stackoverflow. So my new class looks like this and the table looks perfect...

        public class Folder {
     
            public static Folder CreateNew(string folderName) {
                return new Folder {
                    FolderName = folderName,
                    ID = Guid.NewGuid(),
                };
            }
     
            public Guid ID { getset; }
            public string FolderName { getset; }
     
            // Recursive Folder Hierachie
            public Guid FolderID { getset; }
     
            // Navigation properties
            public virtual Folder ParentFolder { getset; }
            public virtual ICollection<Folder> ChildFolders { getset; }
        }
    

    Chen, were is the correct place to post Feedback for EF and the EntiyFramework.Migrations?

    Kind regards and have a good day (or afternoon)!

    Markus

    Wednesday, October 19, 2011 7:43 AM