0

I have just made a model and controller which insert new row in a custom table to my umbraco database.

I based it on petapoco tutorial http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/

Despite the script executing without errors the row is not inserted into the table.

here's what I have:

namespace MyImport.Models
{
[TableName("MyImport_Uploads")]
[PrimaryKey("ID", autoIncrement = true)]
[ExplicitColumns]
public class ImportFile
{
    [Column("ID")]
    [PrimaryKeyColumn(AutoIncrement=true)]
    public int Id { get; set; }

    [Required]
    [Column("CompanyID")]
    public string CompanyId { get; set; }
    //public Guid CompanyId { get; set; }

    [Required]
    [Column("FilenameOriginal")]
    public string FilenameOriginal { get; set; }

    [Required]
    [Column("Filename")]
    public string Filename { get; set; }

    [Required]
    [Column("FileType")]
    public string FileType { get; set; }

    [Column("NumberOfItems")]
    public int NumberOfItems { get; set; }

    [Column("DateCreated")]
    public DateTime DateCreated { get; set; }

    [Column("DeleteExisting")]
    public bool DeleteExisting { get; set; }
}
}

And controller:

namespace MyImport.Controllers
{
    public class ImportController : SurfaceController
    {
        private Umbraco.Core.Persistence.UmbracoDatabase db = MyImport.Settings.UmbracoDbDSN;

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ImportExcel(ImportModel model)
    {

        var fileTypes = new List<string>
        {
            "text/xml",
            "application/xml",
        };
        string fileType = GetFileType(model.FileUpload.ContentType);

        if(model.FileUpload != null && model.FileUpload.ContentLength > 0)
        {
            string uploadDir = "~/imports";
            string origFileName = model.FileUpload.FileName;
            string extension = origFileName.Substring(origFileName.LastIndexOf('.') + 1);
            string pathToCheck = Path.Combine(Server.MapPath(uploadDir), origFileName);

            // Check to see if a file already exists with the
            // same name as the file to upload.
            if (!System.IO.File.Exists(pathToCheck))
            {
                string companyId = MyImport.Member.CompanyIdDummy;

                string tempfileName = companyId.ToLower() + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + extension;
                pathToCheck = Path.Combine(Server.MapPath(uploadDir), tempfileName);
                model.FileUpload.SaveAs(pathToCheck);

                var importFile = new ImportFile
                {
                    CompanyId = companyId,
                    Filename = tempfileName,
                    FilenameOriginal = origFileName,
                    FileType = extension,
                    DateCreated = DateTime.UtcNow
                };

                db.Insert(importFile);
            }

            TempData.Add("Success", true);
        }

        //redirect to current page to clear the form
        return RedirectToCurrentUmbracoPage();
    }
}

Any suggestions? Thanks

4

1 回答 1

0

我错误地设置了数据库。

基本上,我将它作为 umbraco.sdf 放在 App_Data 文件夹中,而我打算使用创建表的完整 MSSQL 数据库。

我重新创建了项目,重新安装了 umbraco 和 HAD 以在我选择设置 cms 访问的屏幕底部选择自定义。这很重要,因为然后我被引导到可以设置数据库连接的屏幕。否则 umbraco 只需安装到存储在项目中 umbraco.sdf 文件中的 SQL Server Express 数据库中。

我希望这对任何寻求解决同一问题的人都有意义。

于 2014-07-11T09:55:06.453 回答