实体框架缺失部分

发布于 2024-12-15 14:03:33 字数 1000 浏览 0 评论 0原文

我正在学习实体框架,我决定创建一个简单的控制台应用程序,该应用程序使用带有两个表的实体框架。

我创建了两个表: http://wstaw.org/w/LrL/

我有合适的数据库的类。

这是代码。

接触类。

public class Contact
{
     public int ContactID { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public ICollection<Address> Addresses { get; set; }
}

还有地址类。

public class Address
{
     public int AddressID { get; set; }
     public string Street { get; set; }
     public string City { get; set; }
}

Visual Studio 显示以下错误。

错误 3 声明时缺少部分修饰符 输入“MyEntityPOCO.Contact”;另一个部分 此类型的声明存在 Contact.c 8 19 MyEntityPOCO

错误 4 类型声明中缺少部分修饰符 'MyEntityPOCO.Address';该类型的另一个部分声明 存在 Address.cs 8 19 MyEntityPOCO

错误 1 ​​运行转换:请覆盖 将标记“$edmxInputFile$”替换为实际名称 您想要生成的 .edmx 文件。 MyEntityPOCO\Model1.tt 1 1

如何解决这些错误?

I am learning the Entity Framework and I decided to create a simple console application that uses Entity framework with two tables.

I've created two tables: http://wstaw.org/w/LrL/

I have appropiate classes for the database.

This is the code.

The Contact class.

public class Contact
{
     public int ContactID { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public ICollection<Address> Addresses { get; set; }
}

And the Address class.

public class Address
{
     public int AddressID { get; set; }
     public string Street { get; set; }
     public string City { get; set; }
}

Visual studio shows Following Errors.

Error 3 Missing partial modifier on declaration of
type 'MyEntityPOCO.Contact'; another partial
declaration of this type exists Contact.c 8 19 MyEntityPOCO

Error 4 Missing partial modifier on declaration of type
'MyEntityPOCO.Address'; another partial declaration of this type
exists Address.cs 8 19 MyEntityPOCO

Error 1 Running transformation: Please overwrite the
replacement token '$edmxInputFile$' with the actual name of the
.edmx file you would like to generate from. MyEntityPOCO\Model1.tt 1 1

How might I resolve these errors?

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

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

发布评论

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

评论(4

泛滥成性 2024-12-22 14:03:33

partial 关键字用于在多个代码文件中声明一个类。此处进行了解释: http://msdn.microsoft.com/en-us/ library/wa80x488.aspx

在您的情况下发生的情况是,您有一个用partial关键字修饰的类(在 yourmodel.edmx.cs 文件中创建的类EF),以及同一命名空间中具有相同名称的另一个类。

通常,如果两个类在同一命名空间中具有相同的名称(没有partial 关键字),您会收到类名冲突异常。在本例中,由于两个类之一被标记为部分类,因此 C# 编译器会生成您所看到的错误。

建议的解决方案:在代码中搜索其他类,确定它是如何到达那里的,然后重命名它,将其移动到不同的命名空间,或者如果它是同一个类,也用部分标记它。

The partial keyword is used to declare a class in more than one code file. It is explained here: http://msdn.microsoft.com/en-us/library/wa80x488.aspx

What has happened in your case is that you have a class decorated with the partial keyword (the one created in the yourmodel.edmx.cs file by EF), and another class with the same name in the same namespace.

Normally with two classes with the same name in the same namespace (without the partial keyword) you would get a class name conflict exception. In this case, because one of the two classes is marked as partial, the C# compiler generates the error that you are seeing.

Suggested solution: Search your code for the other class, determine how it got there and either rename it, move it to a different namespace or also mark it with partial if it is meant to be the same class.

和我恋爱吧 2024-12-22 14:03:33

看起来您正在使用 Database First Entity Framework,因此您创建了数据库并进行了拖放操作以将实体移动到 edmx 中。

您似乎还手动创建了联系人和地址的类。是这样吗?您不需要这样做,因为实体框架将为您生成类。

It looks like you are using Database First Entity Framework, so you created the database and did drag-and-drop to move the entities into an edmx.

It also looks like you might have manually created classes for Contact and Address. Is this the case? You don't need to do this because Entity Framework will generate the classes for you.

感情洁癖 2024-12-22 14:03:33

当您在实体框架中创建实体时,EF 将自动为您创建类并将它们标记为 PARTIAL 类。因此,您可以立即“开箱即用”使用它们。

这就是为什么它说你需要将它们创建为部分类...它已经有一个名为“Address”的类,该类被标记为部分类,因此它认为这个新类应该是该类的另一个“部分”,但你只需忘了这样标记它。

如果您想扩展该类的功能,并在该类中添加函数,您可以执行以下操作:

public partial class Address {...}

但看起来您可能只是尝试再次重新定义该类。

When you create an Entity in Entity Framework, EF will automatically create the classes for you and label them as PARTIAL classes. Therefore, you can use them immediately 'out of the box'.

This is why it's saying you need to create them as partial classes... it already has a class named 'Address' that's labeled as partial, so it thinks this new one should be the other 'part' of that class, but you simply forgot to label it that way.

If you wanted to extend what that class can do, and add functions inside that class, you could do something like:

public partial class Address {...}

but it looks like you may simply be trying to re-define the class again.

北城挽邺 2024-12-22 14:03:33

我遇到了同样的问题,Visual Studio 正在生成 .edmx 文件,但没有生成类。折腾了半天,终于找到了解决办法。

创建项目时使用不同版本的 MVC 框架:
创建项目时尝试使用.net MVC 4.5版本或任何其他版本。

使用不同版本的实体框架
当我添加实体框架数据模型时,我将实体框架版本从 6.x 更改为 5.0。

就是这样。

I had same problem visual studio was generating .edmx file but was not generating classes. After spending half day, I got the solution.

Use different Version of MVC framework while creating project:
Try to use .net MVC 4.5 version or any other version while creating project.

Use different version of entity framework
When I was adding entity framework data model, I changed entity framework version from 6.x to 5.0.

THat's it.

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