开发者

MVC 3 EF Code-first to webhost database trouble

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 开发者_如何学Pythonframework, 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


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜