使Entity Framework生成的实体实现接口

你好我有这个界面:


public interface X {

    int Id { get; set; }

    int Number { get; set; }

}

我想要一个由实体框架生成的具有此属性的实体来实现此接口。我怎么做?我试图做一个部分类,但我得到一个编译错误,迫使我在接口中实现属性,如下所示。


public partial class A : X {

    int Id { get; set; }

    int Number { get; set; }

}

这是实体框架生成的类:


//------------------------------------------------------------------------------

// <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; }

    }

}

我有这些当前文件:


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; }

    }

}


繁华开满天机
浏览 172回答 2
2回答

GCT1015

由于 Entity Framework 生成的类已经包含接口的属性,因此您只需在 class 上声明接口A。整个图片可能包括以下3个文件。确保这些部分类的名称和命名空间匹配,并且这两个.cs文件是同一个 Visual Studio 项目的一部分。接口X.cs(按照惯例,在接口I前面加上IX.)namespace App{&nbsp; &nbsp; public interface X {&nbsp; &nbsp; &nbsp; &nbsp; int Id { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; int Number { get; set; }&nbsp; &nbsp; }}由实体框架生成的类A.cs(保留这个自动生成的原样,如下所示。)//------------------------------------------------------------------------------// <auto-generated>//&nbsp; &nbsp; &nbsp;This code was generated from a template.////&nbsp; &nbsp; &nbsp;Manual changes to this file may cause unexpected behavior in your application.//&nbsp; &nbsp; &nbsp;Manual changes to this file will be overwritten if the code is regenerated.// </auto-generated>//------------------------------------------------------------------------------namespace App{&nbsp; &nbsp; using System;&nbsp; &nbsp; using System.Collections.Generic;&nbsp; &nbsp; public partial class A&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]&nbsp; &nbsp; &nbsp; &nbsp; public A()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int Number { get; set; }&nbsp; &nbsp; }}X例如,类中的接口声明A。A.partial.csnamespace App{&nbsp; &nbsp; public partial class A : X {&nbsp; &nbsp; }}

呼唤远方

public interface IBaseEntity {&nbsp; &nbsp; int Id { get; set; }&nbsp; &nbsp; int Number { get; set; }}假设您有实体框架生成的 StudentEntity.csnamespace MyProject.DAL.Entities{&nbsp; &nbsp; public partial class StudentEntity&nbsp; &nbsp; { }}创建新文件 StudentEntityExtended.cs 并在其中放置您的部分类namespace MyProject.DAL.Entities{&nbsp; &nbsp; public partial class StudentEntity : IBaseEntity {&nbsp; &nbsp; }}然后public class SchoolContext: DbContext&nbsp;{&nbsp; &nbsp; public SchoolContext(): base()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public DbSet<Student> Students { get; set; }}现在,Students DbSet 继承自 BaseClass,因此具有来自其 anchestory 的属性
打开App,查看更多内容
随时随地看视频慕课网APP