c# - 实体注释属性不起作用

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

这是我的实体:

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

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

最佳答案

ColumnAttribute

Name 属性只定义了 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

关于c# - 实体注释属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15437452/

相关文章:

c# - 让 Jenkins 作业更快地从 GIT 下载代码

c# - 如何在 ms access 中以编程方式创建 GUID 自动编号

c# - 通用存储库 - 表达式数组 <Func<T, TProperty>> 错误

c# - 将 2 个表绑定(bind)在一起(一对多关系)

c# - Xamarin:不传播任务引发的异常

c# - 在 Windows 应用程序中的 notifyicon 图标上显示文本

entity-framework - 为什么 DbContext 实现了 IObjectContextAdapter 但没有公共(public) ObjectContext 属性

linq - 在linq联接中使用等于和不等于

entity-framework - 在EF 4.1 Code-First中使用Include和/或Select方法时,如何对导航属性进行排序?

c# - 如何在EF6 Code First中创建枚举对应的表?