C# 为什么EF 6会自动删除实体类中的覆盖方法?
嗯,我在模型类中包含了一个覆盖方法来获取与索引相关的值,例如:City表与Country表相关,因此如果我将gridview数据源设置为table.toList()它会添加id_Country index字段,通过在类中覆盖ToString()方法,可以在网格中设置国家名称的返回,它可以工作,但是过了一段时间,EF模型删除了覆盖方法,并且不再返回值。这是EF使用override方法自动生成的类,但最终将被EF删除:C# 为什么EF 6会自动删除实体类中的覆盖方法?,c#,winforms,entity-framework,C#,Winforms,Entity Framework,嗯,我在模型类中包含了一个覆盖方法来获取与索引相关的值,例如:City表与Country表相关,因此如果我将gridview数据源设置为table.toList()它会添加id_Country index字段,通过在类中覆盖ToString()方法,可以在网格中设置国家名称的返回,它可以工作,但是过了一段时间,EF模型删除了覆盖方法,并且不再返回值。这是EF使用override方法自动生成的类,但最终将被EF删除: public partial class pais { [System.
public partial class pais
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public pais()
{
this.comprador_vendedor = new HashSet<comprador_vendedor>();
this.estado = new HashSet<estado>();
this.exportacion = new HashSet<exportacion>();
}
public int id_pais { get; set; }
public string nombre { get; set; }
public override string ToString()
{
return nombre;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<comprador_vendedor> comprador_vendedor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<estado> estado { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<exportacion> exportacion { get; set; }
}
公共部分类PAI
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共pais()
{
this.compador_vendedor=new HashSet();
this.estado=新的HashSet();
this.exportacion=new HashSet();
}
公共整数id_pais{get;set;}
公共字符串nombre{get;set;}
公共重写字符串ToString()
{
返回nombre;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection买办{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection estado{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection导出{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>
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
//
//------------------------------------------------------------------------------
如果您想扩展该类,只需在同一名称空间中创建另一个同名的分部类请发布您的代码以进行进一步的故障排除。老实说,我在这方面很新,因此,您能给我一些正确方法的示例吗?当然。看看这个答案是怎么做的