Table entity framework 中的数据注释中的名称不起作用。
Table name in data annotations in entity framework doesn't work.
我使用 entity framework6 在 MVC 5 中创建了一个项目。我正在使用代码优先方法。我想在其中一个模型中为 table 定义一个与默认名称不同的名称。为此,我使用 System.ComponentModel.DataAnnotationsname space 并像这样定义 class:
[Table(Name="Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
运行 项目 我得到了一个 table 的数据库,名称为 AuditoriaDALs。为什么 table 这个名字不是我定义的名字?
name= 不是必需的。你应该试试 [Table("Auditoria")].
您可以像下面这样设置 TableName :
public class MyContext : DBContext
{
public virtual DbSet<AuditoriaDAL> Auditorias { get; set; }
}
或在 OnModelCreating 中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AuditoriaDAL>().ToTable("Auditorias");
}
当您需要引用 System.ComponentModel.DataAnnotations.Schema.Table 时,您正在引用 System.Data.Linq.Mapping.Table 属性。所以要么这样做:
[System.ComponentModel.DataAnnotations.Schema.Table("Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
或者更好:
using System.ComponentModel.DataAnnotations.Schema;
...
[Table("Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx
我使用 entity framework6 在 MVC 5 中创建了一个项目。我正在使用代码优先方法。我想在其中一个模型中为 table 定义一个与默认名称不同的名称。为此,我使用 System.ComponentModel.DataAnnotationsname space 并像这样定义 class:
[Table(Name="Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
运行 项目 我得到了一个 table 的数据库,名称为 AuditoriaDALs。为什么 table 这个名字不是我定义的名字?
name= 不是必需的。你应该试试 [Table("Auditoria")].
您可以像下面这样设置 TableName :
public class MyContext : DBContext
{
public virtual DbSet<AuditoriaDAL> Auditorias { get; set; }
}
或在 OnModelCreating 中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AuditoriaDAL>().ToTable("Auditorias");
}
当您需要引用 System.ComponentModel.DataAnnotations.Schema.Table 时,您正在引用 System.Data.Linq.Mapping.Table 属性。所以要么这样做:
[System.ComponentModel.DataAnnotations.Schema.Table("Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
或者更好:
using System.ComponentModel.DataAnnotations.Schema;
...
[Table("Auditoria")]
public class AuditoriaDAL
{
[Key]
public int AuditoriaId { get; set; }
...
}
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx