3

I'm using EF Database First. In the db we have a lot of fk's from one table to another (same). When generating the model, EF uses the other ends table name as the role name. This is a problem. I have made a small example below, to illustrate the problem.

I would like the generated code to use the fk-name, instead of the table name. The model VS is generating now works like this:

aTrip.Location
aTrip.Location1
aTrip.Location2

when I would like to have

aTrip.coming_from
aTrip.arrives_to
aTrip.next_dest

Now, I can change the role names in VS. The problem with that is that if I re-import, all my changes are lost. Also, there will be a lot of tedious renaming, since our db contains a lot of fk's like this example.

What I would like to, is to change how VS generates the model, and use the fk's col name, which is much more correct. Can that be done?

+----------------+                   +-------------+
|    Trip        |                   |Location     |
+----------------+                   |             |
|                |                   +-------------+
|                |                   |  Name       |
|                |*                 1|             |
|   coming_from  ---------------------             |
|                |                   |             |
|                |                   |             |
|                |*                 1|             |
|   arrives_to   ---------------------             |
|                |                   |             |
|                |                   |             |
|                |*                 1|             |
|    next_dest   ---------------------             |
|                |                   |             |
+----------------+                   +-------------+
4

1 回答 1

0

我知道这个问题很老,但我偶然发现了它,并认为我会给出我的意见。

现在,我不知道如何回答有关在将数据库导入模型时更改外键默认命名的具体问题。可能有办法,但我不知道!如果有人这样做,那就去吧,我也想听听。

然而,在这种情况下,我会做的是查看数据库中的更改是如何进行的。这可能不适合您的设置,但如果数据库仅用于这个应用程序,您可以从数据库优先转移到模型优先。

在您从数据库初始导入并生成模型后,我会将模型作为数据库的原始来源,并在那里进行所有更改,并相应地更新数据库。这意味着您可以以任何您想要的方式重命名关系,而不必在每次更改数据库时重复该任务。

我完全理解,如果您的情况比这更复杂,这可能一点也不适用,但它可能是某些人的选择。

当然,您总是可以更进一步,先定义模型代码,我个人更喜欢使用它,但这只是我的意见!

于 2013-08-07T14:19:02.663 回答