Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an entity framework 6 class

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()
        {

        }
    }
}
like image 990
dynamicuser Avatar asked Mar 15 '26 16:03

dynamicuser


1 Answers

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).

like image 116
Sergey Berezovskiy Avatar answered Mar 18 '26 05:03

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!