嗨,我有这个接口:
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; }
}
}
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 {
}
}
由于实体框架生成的类已经包含接口的属性,因此只需在类A
上声明接口。
整个图片可能包括以下3个文件
请确保这些分部类的名称和命名空间匹配,并且两个.cs
文件都是同一Visual Studio项目的一部分
-
接口
X.cs
(按照惯例,在接口前面加上I
,如在IX
中一样。(namespace App { public interface X { int Id { get; set; } int Number { get; set; } } }
-
实体框架
A.cs
生成的类
(保持自动生成的类原样,如下所示。(//------------------------------------------------------------------------------ // <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; } } }
-
A.partial.cs
中A
类上接口X
的声明namespace App { public partial class A : X { } }
public interface IBaseEntity {
int Id { get; set; }
int Number { get; set; }
}
假设您有实体框架生成的StudentEntity.cs
namespace MyProject.DAL.Entities
{
public partial class StudentEntity
{ }
}
创建新的文件StudentEntityExtended.cs,并在其中放入您的分部类
namespace MyProject.DAL.Entities
{
public partial class StudentEntity : IBaseEntity {
}
}
然后
public class SchoolContext: DbContext
{
public SchoolContext(): base()
{
}
public DbSet<Student> Students { get; set; }
}
现在Students DbSet继承自BaseClass,因此具有来自其分支的属性