【发布时间】:2013-02-22 15:58:27
【问题描述】:
我使用“ADO.NET 实体数据模型”创建我的模型类,所以如果我更改我的数据库,我的模型类将会改变..
我的第一堂课来自“ADO.NET 实体数据模型”;
public partial class TableA
{
public TableA()
{
this.TableBs = new HashSet<TableB>();
}
public int TableAID { get; set; }
public string TableAName { get; set; }
public virtual ICollection<TableB> TableBs { get; set; }
}
我的第二堂课来自“ADO.NET 实体数据模型”;
public partial class TableB
{
public TableB()
{
this.TableAs = new HashSet<TableA>();
}
public int TableBID { get; set; }
public string TableBName { get; set; }
public int TableCID { get; set; }
public virtual TableC TableC { get; set; }
public virtual ICollection<TableA> TableAs { get; set; }
}
我的第三课来自“ADO.NET 实体数据模型”;
public partial class TableC
{
public TableC()
{
this.TableBs = new HashSet<TableB>();
}
public int TableCID { get; set; }
public string TableCName { get; set; }
public virtual ICollection<TableB> TableBs { get; set; }
}
还有我的 ViewModel;
public class MyViewModel
{
public TableA tableA { get; set; }
public IEnumerable<TableB> tableBs { get; set; }
public IEnumerable<TableC> tableCs { get; set; }
}
我想在我的 ViewModel 中这样做;
[Required]
[Display(Name = "TableA Name")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string TableAName { get; set; }
我该怎么做??
【问题讨论】:
-
您应该单独创建模型。还有一个包含这三个模型的 viewModel。
-
@AliRızaAdıyahşi,非常正确。最佳实践是拥有数据模型和表示模型。 dustqm,如果您绝对需要 DTO 上的数据注释,请使用伙伴类。
标签: asp.net-mvc entity-framework viewmodel data-annotations