LINQ 实体数据模型生成的代码错误 - 类型“DBContexts.Category”已经包含“ID”的定义;

发布于 2024-08-20 00:25:49 字数 665 浏览 7 评论 0原文

我的数据库中有两个表 - 类别和部门,它们都包含相同的列 - ID、名称和代码。

我可以使用 Visual Studio 2008 设计器创建一个新的实体模型并添加部门,它工作正常 - 我可以使用 LINQ 查询数据库,一切都很好。

当我更新模型并添加类别表时,当我尝试编译时,生成的代码会引发以下错误。

The type 'DBContexts.Category' already contains a definition for 'ID'
The type 'DBContexts.Category' already contains a definition for '_ID'

The type 'DBContexts.Category' already contains a definition for 'Name'
The type 'DBContexts.Category' already contains a definition for '_Name'

A partial method may not have multiple defining declarations

我想这与“部门”和“类别”具有相同的列名称有关 - 但代码列似乎没有问题。

我有什么想法可以解决这个问题吗?我真的不能乱搞生成的代码,因为如果我更新模型,任何更改都将被删除。

干杯

I have two tables in my database - Category and Department which both contain the same columns - ID, Name and Code.

I can create a new entity model using the Visual Studio 2008 designer and add the Department and it works fine - I can query the database using LINQ, alls good.

When I update the model and add the Category table, the generated code throws the following errors when I try and compile.

The type 'DBContexts.Category' already contains a definition for 'ID'
The type 'DBContexts.Category' already contains a definition for '_ID'

The type 'DBContexts.Category' already contains a definition for 'Name'
The type 'DBContexts.Category' already contains a definition for '_Name'

A partial method may not have multiple defining declarations

I guess this is something to do with the fact that Department and Category have identical column names - but it doesn't seam to have a problem with the Code column.

Any ideas how I can fix this? I can't really go messing around with the generated code as any changes will be removed if I update the model.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2024-08-27 00:25:49

当您更新 .dbml 文件时,也会重新生成 .designer.cs 文件,但如果无法访问该文件,则会生成新文件并将其添加到项目中。由于两个文件中的类都是部分的,但在编译时具有相同的属性和字段,因此您将收到此错误。

您可以检查是否只有一个设计器为您的上下文生成的文件。您可以通过右键单击类的名称并选择“转到定义”来完成此操作 - 它将显示包含类定义的所有文件。

When you update the .dbml file the .designer.cs file is also regenerated BUT if this file is not accessible a new file is generated and added to the project. Since the classes in both files are partial but have the same properties and fields on compile you will get this error.

You can check if you have only one designer generated file for your context. You can do that by right clicking on the name of your class and selecting "Go To Definition" - it will show all files containing definition of your class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文