3

Im fairly new to ASP.NET MVC 3, and to coding in general really.

I have a very very small application i want to upload to my webhosting domain.

I am using entity framework, and it works fine on my local machine. I've entered a new connection string to use my remote database instead however it dosen't really work, first of all i have 1 single MSSQL database, which cannot be de dropped and recreated, so i cannot use that strategy in my initializer, i tried to supply null in the strategy, but to no avail, my tables simply does not get created in my database and thats the problem, i don't know how i am to do that with entity framework.

When i run the application, it tries to select the data from the database, that part works fine, i just dont know how to be able to create those tabes in my database through codefirst.

I could probaly get it to work through manually recreating the tables, but i want to know the solution through codefirst.

This is my initializer class

public class EntityInit : DropCreateDatabaseIfModelChanges<NewsContext>
{
    private NewsContext _db = new NewsContext();


    protected override void Seed(NewsContext context)
    {
        new List<News>
        {
            new News{ Author="Michael Brandt", Title="Test News 1 ", NewsBody="Bblablabalblaaaaa1" },
            new News{ Author="Michael Brandt", Title="Test News 2 ", NewsBody="Bblablabalblaaaaa2" },
            new News{ Author="Michael Brandt", Title="Test News 3 ", NewsBody="Bblablabalblaaaaa3" },
            new News{ Author="Michael Brandt", Title="Test News 4 ", NewsBody="Bblablabalblaaaaa4" },
        }.ForEach(a => context.News.Add(a));


        base.Seed(context);
    }
}

As i said, im really new to all this, so excuse me, if im lacking to provide the proper information you need to answer my question, just me know and i will answer it

Alexandre Brisebois
  • 6,599
  • 12
  • 50
  • 69

2 Answers2

2

Initialization strategies do not support upgrade strategies at the moment.

Initialization strategies should be used to initialise a new database. all subsequent changes should be done using scripts at the moment.

the best practice as we speak is to modify the database with a script, and then adjust by hand the code to reflect this change.

in future releases, upgrade / migration strategies will be available.


try to execute the scripts statement by statement from a custom IDatabaseInitializer then from this you can read the database version in the db and apply the missing scripts to your database. simply store a db version in a table. then level up with change scripts.

public class Initializer : IDatabaseInitializer<MyContext>
        {
            public void InitializeDatabase(MyContext context)
            {
                if (!context.Database.Exists() || !context.Database.CompatibleWithModel(false))
                {
                    context.Database.Delete();
                    context.Database.Create();
                    var jobInstanceStateList = EnumExtensions.ConvertEnumToDictionary<JobInstanceStateEnum>().ToList();
                    jobInstanceStateList.ForEach(kvp => context.JobInstanceStateLookup.Add(
                        new JobInstanceStateLookup()
                            {
                                JobInstanceStateLookupId = kvp.Value,
                                Value = kvp.Key
                            }));

                    context.SaveChanges();
                }          
            }
        }

Have you tried to use the CreateDatabaseOnlyIfNotExists

– Every time the context is initialized, database will be recreated if it does not exist.

The database initializer can be set using the SetInitializer method of the Database class.If nothing is specified it will use the CreateDatabaseOnlyIfNotExists class to initialize the database.

Database.SetInitializer(null);

-

Database.SetInitializer<NewsContext>(new CreateDatabaseOnlyIfNotExists<NewsContext>());

I'm not sure if this is the exact syntax as I have not written this in a while. But it should be very similar.

Alexandre Brisebois
  • 6,599
  • 12
  • 50
  • 69
  • I guess it works in the sense it dosent create the database, but it still dont create the tables though, it just moves forward to try query the database in my controller – Paragonbliss Aug 30 '11 at 17:31
  • if your configurations are correct then this will create the tables – Alexandre Brisebois Aug 30 '11 at 17:37
  • Is there any parcticular configuration you're thinking about, so i could show you. I am sorry, as i said, im still very very new to this, so its still all a bit confusing to me, so im still getting alot of things wrong heh. – Paragonbliss Aug 30 '11 at 17:39
  • This is pretty much how my NewsContext is, i have looked through that one which you linked to me actually, the walk through – Paragonbliss Aug 30 '11 at 17:44
  • I've updated my answer this will be your best bet at the moment. – Alexandre Brisebois Aug 30 '11 at 17:54
  • I was mistaken with the configuration, I had miss read the question, quite sorry about that. – Alexandre Brisebois Aug 30 '11 at 17:55
  • Thank you, i ended up just adding the table manually, in the end, it did seem a bit easier, but your information explained some things for me, which i appreciate! – Paragonbliss Aug 30 '11 at 18:18
0

If you are using a very small application, you maybe could go for SQL CE 4.0.
The bin-deployment should allow you to run SQL CE 4.0 even if your provider doesn't have the binaries installed for it. You can read more here.

That we you can actually use whatever initializer you want, since you now don't have the problem of not being able to drop databases and delete tables.

could this be of any help?

Major Byte
  • 4,101
  • 3
  • 26
  • 33
  • hmm realized now that the latest version is done for CTP5, so it could/might be broken. Anyway, I probably am going to check it out myself as well thanks! :) – Major Byte Aug 30 '11 at 17:14
  • Thank you! It tries however to drop all the current tables in my database though? which i can't and shoulden't how do i work around that? – Paragonbliss Aug 30 '11 at 17:18
  • I was a bit too fast there, sorry. Mislead by part of the name "JustCreateTables" ... If you can't drop tables either, then there's not much left then using scripts I guess, if the purpose is updating already existing tables. – Major Byte Aug 30 '11 at 17:28
  • No not existing tables, it has to create new tables, however it tried to delete every table inside my database though – Paragonbliss Aug 30 '11 at 17:32
  • ok, but then how will he persist the data throughout the file time of the application ? Every update will cause the database to get rebuilt ? you still have the same problem... maintenance. you will still be required not to drop this in memory database and run scripts top make it evolve. furthermore, if this is a shared hosting, having an inmemory database may not be the best thing if you have an actual sql server instance available to you. – Alexandre Brisebois Aug 31 '11 at 11:48
  • I realize now I might have misinterpreted the "cannot drop and recreate database" part. I took that as a restriction enforced by the hosting provider, and thought he might circumvent that by using SQL CE 4.0. About the initialization strategies your are absolutely correct, that's why I said in a previous comment that updates should by done by using scripts. – Major Byte Aug 31 '11 at 12:11