实体框架:没有主键的表 [英] Entity Framework: table without primary key

查看:44
本文介绍了实体框架:没有主键的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据库,我想用它使用 EF4.0

I have an existing DB with which I would like to build a new app using EF4.0

有些表没有定义主键,所以当我创建一个新的实体数据模型时,我收到以下消息:

Some tables do not have primary keys defined so that when I create a new Entity Data Model, I get the following message:

The table/view TABLE_NAME does not have a primary key defined 
and no valid primary key could be inferred. This table/view has 
been excluded. To use the entity, you will need to review your schema, 
add the correct keys, and uncomment it.

如果我想使用它们并修改数据,我是否必须向这些表添加一个 PK,或者是否有解决方法以便我不必这样做?

If I want to use them and modify data, must I necessarily add a PK to those tables, or is there a workaround so that I don't have to?

推荐答案

这个错误的意思就是它所说的.

即使你能解决这个问题,相信我,你也不想.可能引入的令人困惑的错误数量惊人且令人恐惧,更不用说您的表现可能会下降的事实.

Even if you could work around this, trust me, you don't want to. The number of confusing bugs that could be introduced is staggering and scary, not to mention the fact that your performance will likely go down the tubes.

不要解决这个问题.修复您的数据模型.

Don't work around this. Fix your data model.

我已经看到很多人反对这个问题.我想这很好,但请记住,OP 询问的是在没有主键的情况下映射,而不是视图.答案还是一样.从可管理性、数据完整性和性能的角度来看,解决 EF 需要对表进行 PK 是一个坏主意.

I've seen that a number of people are downvoting this question. That's fine, I suppose, but keep in mind that the OP asked about mapping a table without a primary key, not a view. The answer is still the same. Working around the EF's need to have a PK on tables is a bad idea from the standpoint of manageability, data integrity, and performance.

有些人评论说他们没有能力修复底层数据模型,因为它们映射到第三方应用程序.这不是一个好主意,因为模型可能会从你的手下改变.可以说,在这种情况下,您可能希望映射到一个视图,这又不是 OP 所要求的.

Some have commented that they do not have the ability to fix the underlying data model because they're mapping to a third-party application. That is not a good idea, as the model can change out from under you. Arguably, in that case, you would want to map to a view, which, again, is not what the OP asked.

这篇关于实体框架:没有主键的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆