1

Hi I have this interface:

public interface X {
    int Id { get; set; }
    int Number { get; set; }
}

And I want an entity generated by Entity Framework that have this properties to implement this interface. How I do it? I tried to do a partial class but I get a compile error that force me to implement the properties in interface as I show below.

public partial class A : X {
    int Id { get; set; }
    int Number { get; set; }
}

This is the class generated by Entity Framework:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace App
{
    using System;
    using System.Collections.Generic;

    public partial class A
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public A()
        {
        }

        public int Id { get; set; }
        public int Number { get; set; }
    }
}

I have these current files:

1.

namespace ConfApp.model
{
    using System;
    using System.Collections.Generic;

    public partial class INSTITUICAO
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public INSTITUICAO()
        {
            this.UTILIZADOR = new HashSet<UTILIZADOR>();
        }

        public int Id { get; set; }
        public string Nome { get; set; }
        public string Morada { get; set; }
        public string Pais { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<UTILIZADOR> UTILIZADOR { get; set; }
    }
}

2.

namespace ConfApp.model {
    public interface IInstituicao {
        int Id { get; set; }
        String Nome { get; set; }
        String Morada { get; set; }
        String Pais { get; set; }
    }
}

3.

namespace ConfApp.model {
    public partial class INSTITUICAO: IInstituicao {
    }
}
18
  • 1
    @Simonare You are saying I should do that in the file generated by the Entity Framework? Commented Dec 19, 2018 at 22:09
  • 1
    @pfx The compiler error says that I should implement the properties Commented Dec 19, 2018 at 22:11
  • 1
    I don't think this is really what you want to do... If it's database first then how would you expect to add properties / columns at run time??? I expect more on course would be generic methods? <TEntity> GetId(this Entity entity){} maybe? Commented Dec 19, 2018 at 22:15
  • 1
    @pfx The post was edited with that class Commented Dec 19, 2018 at 22:25
  • 1
    Are you sure you have both partial classes in the same namespace? Can you show the complete declaration of each including namespace. I assume they are in the same assembly also, right? Commented Dec 19, 2018 at 22:30

2 Answers 2

1

Since your class generated by Entity Framework already contains the properties for the interface, you only have to declare the interface upon class A.

The whole picture might include the following 3 files.
Make sure that the names and namespaces of these partial classes match and that both .cs files are part of the same Visual Studio project.

  1. The interface X.cs
    (By convention, prefix an interface by I as in IX.)

    namespace App
    {
        public interface X {
            int Id { get; set; }
            int Number { get; set; }
        }
    }
    
  2. The class generated by Entity Framework A.cs
    (Leave this auto-generated one as-is, which will look like below.)

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace App
    {
        using System;
        using System.Collections.Generic;
    
        public partial class A
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public A()
            {
            }
    
            public int Id { get; set; }
            public int Number { get; set; }
        }
    }
    
  3. The declaration of the interface X on class A in eg. A.partial.cs

    namespace App
    {
        public partial class A : X {
        }
    }
    
Sign up to request clarification or add additional context in comments.

9 Comments

In point 2 I can't implement interface in the class generated because it has this command //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------
That was I typo on my side; fixed it.
Yes I have that code but got that compile error I was talking about
So you have 3 separate files with the content as above? Try to store file 3 in separate folder, different from the one Entity Framework is using.
Yes I have that
|
0
public interface IBaseEntity {
    int Id { get; set; }
    int Number { get; set; }
}

Suppose that you have StudentEntity.cs generated by Entity Framework

namespace MyProject.DAL.Entities
{
    public partial class StudentEntity
    { }
}

create new file StudentEntityExtended.cs and inside that put your partial class

namespace MyProject.DAL.Entities
{
    public partial class StudentEntity : IBaseEntity {

    }
}

then

public class SchoolContext: DbContext 
{
    public SchoolContext(): base()
    {

    }

    public DbSet<Student> Students { get; set; }
}

Now Students DbSet is inheriting from BaseClass thus having properties from its anchestory

11 Comments

You can't do that because what he's trying to do is adding the implementation of an interface to the partial class generated by Entity Framework
we actually need to ask why he cannot do that? does he have another BaseClass implementation? then why he is not using that? I believe that something is wrong here
I cannot do that because the class generated by EF have a comment saying I should not modify the code like I show above in the edit
You can have another partial class and you can inherit it there. That is why we are using partial classes.
I converted it to interface, now it makes sense :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.