我有 Entity Framework 生成的名为 Person.cs 的类,它位于命名空间 Project.Model 中。
然后我在名为 Extensions 的项目中放置了一个新文件夹,在其中添加了 Person.cs 并将该文件的命名空间设置为 Project.Model。
执行此操作后,出现错误:
Type 'Project.Model.Person' already defines a member called 'Person' with the same parameter types.
我做错了什么?我需要扩展 EF Person.cs 以拥有其他属性。
这是我的扩展 Person.cs 代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project.Model
{
public partial class Person
{
public Person()
{
}
}
}
最佳答案
您应该从 Person
类中删除默认构造函数:
public partial class Person
{
// add properties here
}
您的部分类是同一类的一部分,因此与任何其他类定义一样 - 任何成员都不能定义两次,包括构造函数。如果您转到由 EF 生成的 Person
类,您会看到它已经具有默认构造函数(EF 使用它来初始化导航属性)。
关于c# - 扩展 Entity Framework 6 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605278/