Extending an entity framework 6 class

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

Question

I have the class generated by Entity Framework called Person.cs which is in namespace Project.Model.

I have then put a new folder in the project called Extensions, added Person.cs inside there and set the namespace for this file to Project.Model.

After doing this, I get the error:

Type 'Project.Model.Person' already defines a member called 'Person' with the same parameter types.

What am I doing wrong? I need to extend EF Person.cs to have other properties.

Here is my code for the extended 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()
        {

        }
    }
}
1
3
5/12/2014 9:31:57 AM

Accepted Answer

You should remove default constructor from Person class:

public partial class Person
{
    // add properties here
}

Your partial class is a part of the same class, so as with any other classes definitions - no member can be defined twice, including constructor. If you'll go to Person class generated by EF, you will see that it already has default constructor (EF uses it for navigation properties initialization).

4
5/12/2014 9:30:31 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow