【问题标题】:Entity annotation attributes do not work实体注释属性不起作用
【发布时间】:2013-03-15 16:27:48
【问题描述】:

这是我的实体:

[Table( Name = "PdfMeta" )]
public class Meta
{
    [Key()]
    public int Id { get; set; }

    [Column(Name = "TotalPages")]
    public int TotalPages { get; set; }

    [Column(Name = "PdfPath")]
    public string PdfUri { get; set; }

    [Column(Name = "ImagePath")]
    public string ImageUri { get; set; }

    [Column(Name = "SplittedPdfPath")]
    public string SplittedFolderUri { get; set; }

}

这是来自上下文的代码:

      public DbSet<Meta> PdfMeta { get; set; }

为什么用 ImageUri、PdfUri ... 列创建了新表 (Metas)?我知道这是按惯例完成的,但我已经明确指定了表和列。

【问题讨论】:

  • 你可能使用另外的流利配置?
  • 是的,我使用带有 IDbContext 的通用存储库,...
  • 有两个ColumnAttribute确保你使用的是正确的:System.ComponentModel.DataAnnotations.Schema.ColumnAttribute

标签: c# entity-framework ef-code-first storing-data


【解决方案1】:

ColumnAttributeName 属性仅定义了 getter。而是在构造函数中传递列名:

[Table("PdfMeta")]
public class Meta
{
    [Key]
    public int Id { get; set; }

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

    [Column("PdfPath")]
    public string PdfUri { get; set; }

    [Column("ImagePath")]
    public string ImageUri { get; set; }

    [Column("SplittedPdfPath")]
    public string SplittedFolderUri { get; set; }
}

BTW ColumnAttribute 在 EntityFramework.dll 中定义。看起来您已经从 System.Data.Linq.dll 引用了ColumnAttribute

【讨论】:

  • @NET 可能您使用了错误的属性。验证,它应该来自命名空间 System.ComponentModel.DataAnnotations.Schema 的 EF 程序集
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 2020-02-09
相关资源
最近更新 更多