我使用实体框架codefirst的创建数据库。用模式名dbo.pk_Jobs默认的主键似乎打乱Access 2007年,当我连接到它通过ODBC。如果我手动编辑名称,并删除架构名称,并重新命名该主键pk_jobs,Access现在可以读取表。
我可以指定主键名不包含的架构的使用流利的API,数据属性或任何其他方法的名称。
公共类ReportsContext:的DbContext
{
公共DbSet<工作>乔布斯{获得;组; }
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.Entity<工作>()ToTable(乔布斯)。
modelBuilder.Entity<工作>()HasKey(J => j.uuid)。
base.OnModelCreating(模型构建器);
}
}
公共类职位
{
公众的Guid UUID {获得;组; }
公众诠释活跃{获得;组; }
}
解决方案
如果要指定列名和覆盖属性名称,你可以尝试以下方法:
使用注释
公共类职位
{
[重点,DatabaseGenerated(DatabaseGeneratedOption.Identity)
[专栏(CustomIdName)
公众的Guid UUID {获得;组; }
公众诠释活跃{获得;组; }
}
使用$ C C首先$
保护覆盖无效OnModelCreating(DbModelBuilder MB)
{
base.OnModelCreating(MB);
mb.Entity<工作>()
.HasKey(ⅰ=> i.uuid);
mb.Entity<工作>()
.Property(I => i.uuid)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName(CustomIdName);
}
在迁移配置
公共部分类ChangePrimaryKey:DbMigration
{
公众覆盖无效向上()
{
SQL(@EXEC sp_rename可以'SchemaName.TableName.IndexName,New_IndexName,索引);
}
公众覆盖无效向下()
{
SQL(@EXEC sp_rename可以'SchemaName.TableName.New_IndexName,Old_IndexName,索引);
}
}
I'm using Entity Framework Codefirst to create my Database. The default Primary key with the schema name dbo.pk_Jobs seems to upset access 2007 when I connect to it over ODBC. If I manually edit the name and remove the schema name and rename this Primary Key to pk_jobs, Access can now read the table.
Can I specify the Primary Key name to not include the name of the schema using Fluent Api, Data Attributes or any other method.
public class ReportsContext : DbContext
{
public DbSet<Job> Jobs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Job>().ToTable("Jobs");
modelBuilder.Entity<Job>().HasKey(j => j.uuid);
base.OnModelCreating(modelBuilder);
}
}
public class Job
{
public Guid uuid{ get; set; }
public int active{ get; set; }
}
解决方案
If you want to specify the column name and override the property name, you can try the following:
Using Annotations
public class Job
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("CustomIdName")]
public Guid uuid { get; set; }
public int active { get; set; }
}
Using Code First
protected override void OnModelCreating(DbModelBuilder mb)
{
base.OnModelCreating(mb);
mb.Entity<Job>()
.HasKey(i => i.uuid);
mb.Entity<Job>()
.Property(i => i.uuid)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("CustomIdName");
}
Inside Migration Configuration
public partial class ChangePrimaryKey : DbMigration
{
public override void Up()
{
Sql(@"exec sp_rename 'SchemaName.TableName.IndexName', 'New_IndexName', 'INDEX'");
}
public override void Down()
{
Sql(@"exec sp_rename 'SchemaName.TableName.New_IndexName', 'Old_IndexName', 'INDEX'");
}
}
相关推荐
最新文章