谷歌搜索了很多之后,我发现由于 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 文件以获取更新的数据库,您的代码将不会被删除。