0

我是 Umbraco CMS 的新手。我在我的项目中使用 Ui-O-Matic 插件。
Ui-O-Matic 允许对数据库进行简单的 CRUD 操作。但我想使用后台控件,如文件、文本区域等。

所以我在 database.cs 文件中使用这样的 UIOmaticFielld。

 [Column("newsDetail")]
 [UIOMaticField("News Detail","Add Details",View ="textarea")]
 public string newsDetail { get; set; }

 [Column("newsImage")]
 [UIOMaticField("Image","Upload Image",View ="file")]
 public string newsImage { get; set; }

问题是当我对数据库进行任何更改时,我必须刷新 database.tt 文件以获取数据库更改。但它重新创建了 database.cs 文件和我之前的更改:

[UIOMaticField("News Detail","Add Details",View ="textarea")]

从 database.cs 文件中删除。每次我都必须做同样的改变。
请指导我应该怎么做才能保持我的自定义更改,因为即使我刷新 database.tt 文件也是如此?
其他更好的方式来做 CRUD 操作也是可取的。

4

1 回答 1

1

谷歌搜索了很多之后,我发现由于 database.cs 文件是自动生成的,我不能在其中进行自定义更改。
我找到了另一种使用后台控制的方法。让我在这里解释一下,可能会对其他人有所帮助。

不要在 databse.cs 文件中编写 UIOMatoicField,而是创建模型来执行相同的操作。
在“Models/Generated/database.tt”文件中进行以下更改

// Settings 
    ConnectionStringName = "umbracoDbDSN";          // Uses last connection string in config if not specified
    Namespace = "Generator";
    RepoName = "";
    GeneratePocos = true;
    ClassPrefix = "";  
    ClassSuffix = "";  

    // Read schema 
    var tables = LoadTables();  
    tables["Course"].Ignore = true; // Prevents table to include in databse.cs file  
    tables["News"].Ignore = true; 
if (tables.Count>0)
    {
#>
<#@ include file="UIOMatic.Generator.ttinclude" #>
<# } #>

然后如下创建新模型。例如。“模型\NewsModel.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UIOMatic.Attributes;
using UIOMatic.Enums;
using UIOMatic.Interfaces;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;

namespace EdumentUIOMatic.Models
{
    [Umbraco.Core.Persistence.TableName("News")]
    [PrimaryKey("newsId")]
    [UIOMatic("News", "icon-box-open", "icon-box-open", RenderType = UIOMaticRenderType.List, ConnectionStringName = "umbracoDbDSN")]   
    public class NewsModel: IUIOMaticModel
    {
        [UIOMaticIgnoreField]
        [Column("newsId")]
        public int newsId { get; set; }

        [Column("newsTitle")]
        [UIOMaticField("News Title", "Add Title")]
        public string newsTitle { get; set; }

        [Column("newsDetail")]
        [UIOMaticField("News Detail", "Add Details", View = "textarea")]
        public string newsDetail { get; set; }

        [Column("newsImage")]
        [UIOMaticField("Image", "Upload Image", View = "file")]
        public string newsImage { get; set; }



        [Column("isDeleted")]
        [UIOMaticField("Hide News", "Check if you want to hide this news")]        
        public bool isDeleted { get; set; }



        [System.Web.Http.AcceptVerbs("GET", "POST")]
        public IEnumerable<Exception> Validate()
        {
            return new List<Exception>();
        }
    }
}

现在,如果您将重新加载 database.tt 文件以获取更新的数据库,您的代码将不会被删除。

于 2017-04-14T05:54:57.483 回答