umbraco 7在自定义表中插入行



我刚刚制作了一个模型和控制器,它在我的umbraco数据库的自定义表中插入新行。

我根据petapoco教程http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/

尽管脚本执行时没有出现错误,但该行不会插入到表中。

这是我的

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; }
}
}

控制器:

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();
    }
}

有什么建议吗?感谢

我错误地设置了数据库。

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

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

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

相关内容

  • 没有找到相关文章

最新更新