gpt4 book ai didi

c# - 值不能为空。参数名称 : entitySet

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:53 26 4
gpt4 key购买 nike

<分区>

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Church_On_The_Rock.Models
{
public class News
{
public int NewsId { get; set; }
public string Title { get; set; }
public string Writer { get; set; }
[DisplayFormat(DataFormatString = "{0:MMMM dd, yyyy}",
ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
public byte[] Picture { get; set; }

public string Blog { get; set; }

}
//I know the problem is here but don't know what to do
public class ExtendedNewsModel: News
{
public HttpPostedFileBase postedPicture { get; set; }
}

}

这是我的数据访问代码:

public ActionResult Create(ExtendedNewsModel news)
{
if (ModelState.IsValid)
{
if (news.postedPicture != null)
{

if (news.postedPicture.ContentLength > (8 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "Image can not be larger than 8MB.");
return View();
}
if (!(news.postedPicture.ContentType == "image/jpeg" || news.postedPicture.ContentType == "image/png" || news.postedPicture.ContentType == "image/gif"))
{
ModelState.AddModelError("CustomError", "Image must be in jpeg,png or gif format");
}

}
byte[] data = new byte[news.postedPicture.ContentLength];
news.postedPicture.InputStream.Read(data, 0, news.postedPicture.ContentLength);
news.Picture = data;
db.New.Add(news);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(news);
}

这是我的看法:

 @model Church_On_The_Rock.Models.ExtendedNewsModel

@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm("Create","Home", FormMethod.Post, new {role = "form", enctype = "multipart/form-data"}))
{

@Html.AntiForgeryToken()
<div class="form-group createforms">
<span>@Html.LabelFor(model => model.Picture, htmlAttributes: new {
@class = "Control-label col-md-2" })</span>
<div class="col-md-10">
@Html.TextBoxFor(model => model.postedPicture, new {type
="file" })
@Html.ValidationMessage("customMessage")
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10 createbutton">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}

每次更新数据库或在浏览器中运行代码时,都会出现此错误:

"Value cannot be null. Parameter name: entitySet".

这里是调用栈:

      System.ArgumentNullException: Value cannot be null.
Parameter name: entitySet
at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
at System.Data.Entity.Core.Mapping.EntitySetMapping..ctor(EntitySet entitySet, EntityContainerMapping containerMapping)
at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.AddEntitySetMapping(DbDatabaseMapping databaseMapping, EntitySet entitySet)
at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

但是当我注释掉 The class ExtendedNewsModel 时,它会更新数据库并在浏览器中运行。

我想知道我做错了什么,或者是否有另一种方法可以只使用 HttpPostedFileBase 而无需将其放入类中,以及如何将其呈现给 View 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com