我有一个数据库优先的ASP.NET MVC应用程序和从数据库生成的实体模型,但我需要添加一些东西到未生成的模型。像这样以正确显示日期:实体框架模型和MVC模型不能映射
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
所以我想创造另一种模式,在Models
文件夹,并复制该EDMX了,并添加什么,我需要完全一样的东西。但是这会产生一个问题,我无法将对象从EntityModels.Movie
转换为Models.Movie
,我该如何实现这一目标?
我试过automapper,但并没有真正的工作,我读过的文档是针对旧版本(pre v5),它有一些过时的功能。 这里是我的实体模型代码:
namespace Movies.EntityModels
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public Nullable<System.DateTime> ReleaseDate { get; set; }
public Nullable<int> ID_Genre { get; set; }
public decimal Price { get; set; }
public string Rating { get; set; }
public int Quantity { get; set; }
public virtual Genre Genre { get; set; }
}
}
什么是达到我的目标,最好的办法?
*我试过automapper,但并没有真正的工作* - 什么,所以你说的automapper不起作用?你可以在这里报告错误:https://github.com/AutoMapper/AutoMapper/issues –
为什么不在你的部分类中添加你想要的字段/属性?在使用实体框架时这是相当普遍的做法。原因是生成的模型已经有一个部分修饰符。 –