简体   繁体   English

实体框架PK名称

[英]Entity Framework PK Name

I work with entity framework 6.0 with code first. 我先使用代码编写实体框架6.0。

I need to connect to database table created by Microsoft Access ODBC. 我需要连接到Microsoft Access ODBC创建的数据库表。

When I try to connect to a table, a receive an error about the PK name. 当我尝试连接到表时,收到关于PK名称的错误。

Entity Framework create a PK with name PK_dbo_TABLENAME that create problem with Microsoft Access. 实体框架创建名称为PK_dbo_TABLENAME的PK,从而导致Microsoft Access出现问题。 If I rename it, there are no problem. 如果重命名,就没有问题。

How I can force different name to entity framework key? 如何强制将不同名称用作实体框架密钥?

I search in data-annotation syntax but with no result. 我搜索了数据注释语法,但没有结果。

Thanks. 谢谢。

If you are saying that your column name is not the same name as the property you're using (ie TableID vs Id ) you can use the [Column()] attribute to override the name: 如果您说您的列名与所使用的属性名称不同(即TableIDId ),则可以使用[Column()]属性覆盖该名称:

[Column("GeoLocationDetailId")]
 public Guid Id { get; set; }

If you're referring to the schema not recognize your Primary Key, you can add the [Key] attribute on that property and it will pick it up : 如果您引用的模式无法识别您的主键,则可以在该属性上添加[Key]属性,它将选择它:

    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }

If you're using Model configuration, you can use the HasKey method in the same manner you would with the [Key] attribute: 如果使用模型配置,则可以使用与[Key]属性相同的方式使用HasKey方法:

        modelBuilder.Entity<GeoLocationEntity>()
            .HasKey(e => e.Id)

最后,我从Sql Management Studio中手动重命名PK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM