如何使用 entity framework 核心调用没有主键的表
How call tables without primary key with entity framework core
我正在用 odata 做一个 asp.net 核心 api。我在数据库中的表没有主键,我没有更改数据库的权限。我使用以下命令调用我的数据库:
"dotnet ef dbcontext scaffold "Data Source = 192.168.11.1\sql2016;Initial Catalog=SeeMiddle;persist security info=True;user id=Iaas;password=Iaas123!@#" Microsoft.EntityframeworkCore.SqlServer -d -c SeeMiddleContext -o Models\Entities --force"
我在每个表中都遇到以下错误:
Unable to identify the primary key for table 'cmn.ReshteNerkhItem'.
Unable to generate entity type for table 'cmn.ReshteNerkhItem'.
如何使用在 asp.net 核心中具有任何主键的数据库?
简短的回答,你不知道。 Entity Framework 需要密钥。好消息是,如果您的 table 没有密钥,您可以有效地伪造密钥。如果您的 table 有一个不同的隐式键,那么只需用 [Key]
装饰 属性 就可以了。 key 的事情是它必须是一个独特的唯一值。如果您没有单独的列来执行此操作,那么您将需要开始一起使用列来制作复合键 ([Key, Column(Order = 0)]
.
我正在用 odata 做一个 asp.net 核心 api。我在数据库中的表没有主键,我没有更改数据库的权限。我使用以下命令调用我的数据库:
"dotnet ef dbcontext scaffold "Data Source = 192.168.11.1\sql2016;Initial Catalog=SeeMiddle;persist security info=True;user id=Iaas;password=Iaas123!@#" Microsoft.EntityframeworkCore.SqlServer -d -c SeeMiddleContext -o Models\Entities --force"
我在每个表中都遇到以下错误:
Unable to identify the primary key for table 'cmn.ReshteNerkhItem'.
Unable to generate entity type for table 'cmn.ReshteNerkhItem'.
如何使用在 asp.net 核心中具有任何主键的数据库?
简短的回答,你不知道。 Entity Framework 需要密钥。好消息是,如果您的 table 没有密钥,您可以有效地伪造密钥。如果您的 table 有一个不同的隐式键,那么只需用 [Key]
装饰 属性 就可以了。 key 的事情是它必须是一个独特的唯一值。如果您没有单独的列来执行此操作,那么您将需要开始一起使用列来制作复合键 ([Key, Column(Order = 0)]
.