【发布时间】:2016-06-27 00:10:34
【问题描述】:
我正在使用带有 EF6 的 MVC5。我收到以下转换错误
不能隐式转换类型
System.Collections.Generic.List<TreaceabilitySystem.GLB_M_PROFITCENTER>至System.Collections.Generic.List<TreaceabilitySystem.Models.Profitcenter>
private TSEntities db = new TSEntities();
// GET: Profitcenter
public ActionResult Index()
{
List<Profitcenter> profitcenter = new List<Profitcenter>();
profitcenter = db.GLB_M_PROFITCENTER.ToList(); //Error coming up here
return View(profitcenter.ToList());
}
我的模型在这里: 当我在 .edmx 中添加表格时,这个模型是通过 EF 创建的
public partial class GLB_M_PROFITCENTER
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public GLB_M_PROFITCENTER()
{
this.GLB_M_USERMASTER = new HashSet<GLB_M_USERMASTER>();
}
public string PROFITCENTER_CODE { get; set; }
public string PROFITCENTER_NAME { get; set; }
public string DESCRIPTION { get; set; }
public bool ISACTIVE { get; set; }
public int CREATEDBY { get; set; }
public System.DateTime CREATED_DATE { get; set; }
public Nullable<int> UPDATEDBY { get; set; }
public Nullable<System.DateTime> UPDATED_DATETIME { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<GLB_M_USERMASTER> GLB_M_USERMASTER { get; set; }
}
我创建了以下模型用于更改显示名称和验证目的
[MetadataType(typeof(Profitcenter))]
public partial class GLB_M_PROFITCENTER { }
public class Profitcenter
{
[Required(ErrorMessage = "*")]
[DisplayName("Profitcenter Code")]
public string PROFITCENTER_CODE { get; set; }
[Required(ErrorMessage = "*")]
[DisplayName("Profitcenter Name")]
public string PROFITCENTER_NAME { get; set; }
[DisplayName("Description")]
public string DESCRIPTION { get; set; }
[DisplayName("Is Active")]
public bool ISACTIVE { get; set; }
[DisplayName("Created By")]
public int CREATEDBY { get; set; }
[DisplayName("Created Timestamp")]
public System.DateTime CREATED_DATE { get; set; }
[DisplayName("Upated by")]
public Nullable<int> UPDATEDBY { get; set; }
[DisplayName("Updated Timestamp")]
public DateTime UPDATED_DATETIME
{
get; set;
}
}
两个型号完全一样,我有什么遗漏吗? 我该如何解决这个问题?
【问题讨论】:
-
两个型号怎么一模一样?它们对我来说看起来不同......一个是部分类,另一个是带有嵌套类的部分类。此外,命名空间不同 -
TreaceabilitySystem.GLB_M_PROFITCENTER和TreaceabilitySystem.Models.Profitcenter。即使这些类是相同的(它们不是),编译器也会将它们视为不同的类。 -
这两个属性只是相同的,对吧?我只使用属性
-
命名空间使它们不同。
TraceabilitySystem.GLB_M_PROFITCENTER.PROFITCENTER_CODE与TraceabilitySystem.Models.ProfitCenter.PROFITCENTER_CODE不同。 -
但是这个错误与此有关吗?我该如何解决?
-
此外,属性不相同。如果它们相同,您就不必复制该类。它们是故意不同的。区别在于它们的属性。
标签: c# asp.net-mvc entity-framework