c# - 扩展 Entity Framework 6 类

标签 c# entity-framework entity-framework-6 partial-classes

我有 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/

相关文章:

c# - 如何将 Icon 转换为 base64 字符串?

c# - 部署 Windows 窗体应用程序时要采取的步骤?

c# - 将 C 中包含 unsigned char 指针的代码移植到 C#

Entity Framework 迁移脚本中的 MySql 存储过程/循环

c# - 如何在 Entity Framework 6 代码优先方法中映射选择存储过程?

oracle - 首先使用 Entity Framework 6 代码访问 Oracle Synonyms

javascript - 将 C# List<string> 转换为 Javascript

.net - 是否可以从实体数据模型 (edmx) 文件生成数据库?

c# - GridView 中的复杂对象

c# - 问题单元测试 EF 和 ASP.NET Identity - NotSupportedException : Model Compatibility