简体   繁体   English

如何在没有主键的情况下使用实体框架核心调用表

[英]How call tables without primary key with entity framework core

I am doing an asp.net core api with odata. 我正在用odata做一个asp.net核心api。 My tables in database has no primary key and I do not have permission to change the database. 我数据库中的表没有主键,并且我无权更改数据库。 I use below command to call my database: 我使用下面的命令来调用我的数据库:

"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" “” dotnet ef dbcontext支架“数据源= 192.168.11.1 \\ sql2016;初始目录= SeeMiddle;持久安全信息= True;用户ID = Iaas;密码= Iaas123!@#” Microsoft.EntityframeworkCore.SqlServer -d -c SeeMiddleContext -o模型\\实体-力“

and I encounter below errors for every tables: 并且我在每个表中遇到以下错误:

Unable to identify the primary key for table 'cmn.ReshteNerkhItem'. 无法识别表'cmn.ReshteNerkhItem'的主键。

Unable to generate entity type for table 'cmn.ReshteNerkhItem'. 无法为表'cmn.ReshteNerkhItem'生成实体类型。

how can I work with a database that has any primary key in asp.net core?? 我如何使用在asp.net核心中具有任何主键的数据库?

Short answer, you don't. 简短的答案,你没有。 Entity Framework requires a key. 实体框架需要一个密钥。 Good news is you can effectively spoof a key if your table doesn't have one. 好消息是,如果您的桌子没有钥匙,您可以有效地欺骗它。 If your table has an implicit key that is distinct then just decorate that property with [Key] and you'll be good. 如果您的表具有一个不同的隐式键,则只需用[Key]装饰该属性,就可以了。 The key thing is that it has to be a distinct unique value. 关键是它必须是一个独特的独特价值。 If you do not have a singular column that does that then you'll need to start using columns together to make a composite key ( [Key, Column(Order = 0)] . 如果您没有做到这一点的单数列,那么您将需要开始同时使用各列来制作复合键( [Key, Column(Order = 0)]

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

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