ModelValidationException - MVC Codefirst

时间:2015-07-29 10:39:21

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 asp.net-mvc-5

我使用实体框架创建了控制器它给出了错误我从下面的类中评论了HttpPostedFileBase属性并再次尝试,它工作并且生成了所有CRUD视图。现在,当我尝试使用以下代码添加新工作站时:

    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public ActionResult Create(Station station)
    {
        station.FileName = station.File.FileName;
        station.ImageSize = station.File.ContentLength;
        byte[] data = new byte[station.File.ContentLength];
        station.File.InputStream.Read(data, 0, station.File.ContentLength);
        station.ImageData = data;

            db.Stations.Add(station);
            db.SaveChanges();

        return RedirectToAction("Index");
    }

它给出了异常(如下所示):

  

System.Data.Entity.ModelConfiguration.ModelValidationException是   用户代码未处理HResult = -2146233088消息=一个或多个   在模型生成期间检测到验证错误:

     

MetroTrain.Models.HttpPostedFileBase :: EntityType   ' HttpPostedFileBase'没有定义键。为此定义密钥   的EntityType。 HttpPostedFileBases:EntityType:EntitySet   ' HttpPostedFileBases'基于类型' HttpPostedFileBase'有   没有定义键。

     

Source = EntityFramework StackTrace:          在System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()          在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)          在System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)          在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext)   internalContext)          在System.Data.Entity.Internal.RetryLazy 2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet 1.Initialize()          在System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet 1.ActOnSet(动作,   EntityState newState,Object实体,String methodName)          在System.Data.Entity.Internal.Linq.InternalSet 1.Add(Object entity) at System.Data.Entity.DbSet 1.添加(TEntity实体)          在MetroTrain.Controllers.StationsController.Create(站台)c:\ Users \ ghousia   PC \桌面\ MetroTrain \ MetroTrain \ \控制器StationsController.cs:行   58          在lambda_method(Closure,ControllerBase,Object [])          在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)          在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2   参数)          在System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()          在System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult)   asyncResult,ActionInvocation innerInvokeState)          在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase 1.End()          在System.Web.Mvc.Async.AsyncResultWrapper.End [TResult](IAsyncResult)   asyncResult,Object标签)          在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult)   asyncResult)          在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3c()          在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass45.b__3e()   的InnerException:

使用的类(用于脚手架):

 public class Station
    {
        [Key]
        public int Id { get; set; }
        [Display(Name="Station Name")]
        public string StationName { get; set; }
        public double Distance { get; set; }
        [DataType(DataType.PhoneNumber)]
        public string Mobile { get; set; }
        [DataType(DataType.Date)]
        public DateTime Opening { get; set; }
        public string Connections { get; set; }
        public string Layout { get; set; }
        [Display(Name="Tourists Place")]
        public string TouristsPlace { get; set; }
        [Display(Name = "Tourists Place Image")]
        /////////
        public int ImageId { get; set; }
        public Nullable<int> ImageSize { get; set; }
        public string FileName { get; set; }
        public byte[] ImageData { get; set; }

        [Required(ErrorMessage = "Please select file")]
        public HttpPostedFileBase File { get; set; }
        /// <summary>
        /// ////
        /// </summary>
        [Display(Name="Line Color")]
        public string LineColor { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

确保您添加 [NotMapped] 这样的属性。

[NotMapped]
public HttpPostedFileBase File { get; set; }