Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ram's Reddy 10 posts 80 karma points
    Dec 17, 2018 @ 16:02
    Ram's Reddy
    0

    Umbraco Cloud Database Customization

    I want to add some more tables to my existing cloud database. I selected the SQL Server CE as my database connection type. So it creates the Umbraco.sdf file in App_Data folder. So what can I do for this to add and update new tables?

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Dec 17, 2018 @ 16:47
    Nik
    2

    Hi Ram,

    If you want to deploy Database customisation to Umbraco Cloud, I highly recommend using Migrations.

    Although dated, this is a good intro to the concept and I think quite a bit is still valid.

    https://cultiv.nl/blog/using-umbraco-migrations-to-deploy-changes/

    You'll also want to look into PetaPoco as that is the ORM that is used in Umbraco and you can utilise that to create tables based on classes.

    If you take this approach deployments will go through each of your Umbraco Cloud environments when you deploy and it should also work with your local version as well.

    Nik

  • Ram's Reddy 10 posts 80 karma points
    Dec 18, 2018 @ 12:18
    Ram's Reddy
    0

    Thank you, Nik. Actually, I'm trying to connect my Umbraco.sdf file with below code.

    var dbFactory = new OrmLiteConnectionFactory(System.Configuration.ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString, SqlServerDialect.Provider);
            using (var db = dbFactory.Open())
            {
                db.CreateTableIfNotExists<Contact>();
                db.Insert(new Contact
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    HomePhone = model.HomePhone,
                    MobilePhone = model.MobilePhone,
                    ContactMenthod = model.ContactMenthod,
                    Comments = model.Comments
                });
            }
    

    But i'm unable to open connection with Umbraco.sdf. Can you please suggest some better approch.

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Dec 18, 2018 @ 13:14
    Niels Hartvig
    1

    Hi Ram!

    If you follow the docs on how to work with databases on Umbraco Cloud it should be smooth sailing: https://our.umbraco.com/documentation/Umbraco-Cloud/Databases/#using-custom-tables-with-umbraco-cloud

    Hope this helps!

    Niels...

  • Ram's Reddy 10 posts 80 karma points
    Dec 20, 2018 @ 17:00
    Ram's Reddy
    0

    Thank you Niels

  • Ram's Reddy 10 posts 80 karma points
    Jan 03, 2019 @ 17:54
    Ram's Reddy
    0

    Hi Niel,

    I'm trying to connect my Umbraco.sdf file in the cloud. I get the error to open the connection as

    System.Data.SqlServerCe.SqlCeException (0x80004005): The database file cannot be found. Check the path to the database. [ Data Source = C:\home\site\wwwroot\App_Data\Umbraco.sdf ] at System.Data.SqlServerCe.SqlCeConnection.Open(Boolean silent)

    But its working good in my local solution. Here is my controller code...

       [HttpPost]
        public ActionResult ContactForm(Contact model)
        {
            var mainpage = Umbraco.TypedContentAtRoot().FirstOrDefault(n => n.DocumentTypeAlias == "home");
            var database = System.Web.HttpContext.Current.Server.MapPath("~\\App_Data\\Umbraco.sdf");
            using (var conn = new SqlCeConnection("Data Source=" + database))
            {
                conn.Open();
                try
                {
                    string existsQuery = string.Format("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'Contact'");
                    SqlCeCommand command = new SqlCeCommand(existsQuery, conn);
                    var count = Convert.ToInt32(command.ExecuteScalar());
                    if (count == 0)
                    {
                        string createQuery = "Create table [Contact]([FirstName] nvarchar(50), [LastName] nvarchar(50),[Email] nvarchar(50),[HomePhone] nvarchar(50),[MobilePhone] nvarchar(50),[ContactMenthod] nvarchar(50),[Comments] nvarchar(256))";
                        SqlCeCommand command1 = new SqlCeCommand(createQuery, conn);
                        command1.ExecuteNonQuery();
                    }
                    string insertQuery = "INSERT INTO [Contact] ([FirstName],[LastName],[Email],[HomePhone],[MobilePhone],[ContactMenthod],[Comments]) Values('" + model.FirstName + "','" + model.LastName + "','" + model.Email + "','" + model.HomePhone + "','" + model.MobilePhone + "','" + model.ContactMenthod + "','" + model.Comments + "')";
                    SqlCeCommand command2 = new SqlCeCommand(insertQuery, conn);
                    command2.ExecuteNonQuery();
                    EmailSendContact(model);
                    return RedirectToUmbracoPage(mainpage.Id);
                }
                catch (SqlCeException ex)
                {
                    string _errorMsg = ex.Message;
                    return CurrentUmbracoPage();
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }
        }
    

    How can i get the Umbraco.sdf file path in the cloud.

Please Sign in or register to post replies

Write your reply to:

Draft