What is a best practice to load initial data into entities?

for example i have simple MyEntity:

[Serializable]
    [HierarchyRoot]
    public class MyEntity : Entity
    {
        public MyEntity(int id):base (id){}

        [Field, Key]
        public int Id { get; private set; }

        [Field(Length = 100)]
        public string Text { get; set; }
    }

and 2 initial entities to create:

public class MyEntityIntitialData
    {
        public void Populate()
        {
            var MyEntity1 = new MyEntity(1) {Text = "Text1"};
            var MyEntity2 = new MyEntity(2) {Text = "Text2"};
        }
    }

after creating database, and adding some User-data, i create new version of populate method with 3 entities:

public class MyEntityIntitialData
    {
        public void Populate()
        {
            var MyEntity1 = new MyEntity(1) {Text = "Text1"};
            var MyEntity2 = new MyEntity(2) {Text = "Text2"};
            var MyEntity3 = new MyEntity(3) {Text = "Text3"};
        }
    }

So, i don't want to recreate database, but i want something like schema upgrade for Data to add MyEntity3 without deleting 1 and 2. What is a best practice for this?

This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Mar 16 '10 at 13:58

pil0t's gravatar image

pil0t
207575763


One Answer:

Yes:

  • See DomainBuilder.PopulateData method in ASP.NET Sample. It shows how to populate the data during first start only. Consequently, the same approach can be used to add more data on the subsequent start, because first and subsequent starts are distinguishable there.

  • See Database schema upgrade chapter in Manual, and Upgrader class in 3rd Upgrade sample - it shows how to handle data upgrade manually (e.g. add something new or transform the old data). Or, better, look up all 3 upgrade samples.

answered Mar 16 '10 at 15:24

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA