嗨,我有这个界面:
public interface X {
int Id { get; set; }
int Number { get; set; }
}
我想要由Entity Framework生成的具有此属性的实体来实现此接口。 我该怎么做? 我试图做一个局部类,但遇到编译错误,迫使我在接口中实现属性,如下所示。
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 {
}
}
答案 0 :(得分:1)
由于由Entity Framework生成的类已经包含该接口的属性,因此只需要在类A
上声明该接口。
整个图片可能包含以下3个文件。
确保这些部分类的名称和名称空间匹配,并且两个.cs
文件都属于同一Visual Studio项目。
接口X.cs
(按照惯例,在接口{{1}的前面加上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; }
}
}
中接口X
的声明。 A
A.partial.cs
答案 1 :(得分:0)
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; }
}
现在,学生DbSet从BaseClass继承,因此具有其锚点的属性