Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用实体框架和ObjectDataSource更新ASP.NET GridView_Asp.net_Entity Framework_Gridview_Objectdatasource - Fatal编程技术网

使用实体框架和ObjectDataSource更新ASP.NET GridView

使用实体框架和ObjectDataSource更新ASP.NET GridView,asp.net,entity-framework,gridview,objectdatasource,Asp.net,Entity Framework,Gridview,Objectdatasource,我不熟悉实体框架和ObjectDataSource。我试图让我的网格更新一个项目,但出现以下错误。我做错了什么 Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please revi

我不熟悉实体框架和ObjectDataSource。我试图让我的网格更新一个项目,但出现以下错误。我做错了什么

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] GalleryApp.Logic.FileActions.UpdateFile(GalleryItemFile newFile) +377 [TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +92 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +108 System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +487 System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method) +39 System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +1830 System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1210 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642610 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18056 在调试期间,我验证了origFile是否被正确检索。它是从ObjectDataSource发送的新文件,似乎生成了空引用

编辑: 这是GalleryItemFile的模型


同样,我对实体框架是新手,但我认为它会自动加载galleryItem

namespace GalleryApp.Models
{
[Serializable]
public class GalleryItemFile
{
    [Key]
    public int fileID { get; set; }
    public virtual GalleryItem galleryItem { get; set; }
    public virtual FileType fileType { get; set; }
    [MaxLength(75)] public string filename { get; set; }
    public bool active { get; set; }
    public DateTime? createdOn { get; set; }
    [MaxLength(150)] public string createdBy { get; set; }
    [MaxLength(50)] public string resolution { get; set; }

}
}


namespace GalleryApp.Models
{
[Serializable]
public class FileType
{
    [Key]
    public int fileTypeID { get; set; }
    [MaxLength(50)] public string fileType { get; set; }
    [MaxLength(15)] public string extension { get; set; }
    [MaxLength(250)] public string icon { get; set; }
    [MaxLength(400)] public string description { get; set; }
    [MaxLength(400)] public string warnings { get; set; }

}
}
“/”应用程序中出现服务器错误。 对象引用未设置为对象的实例。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。 源错误: 第160行:{ 第161行:origFile.galleryItem.galleryItemID=newFile.galleryItem.galleryItemID; 第162行:origFile.fileType.fileTypeID=newFile.fileType.fileTypeID; 第163行:origFile.filename=newFile.filename; 第164行:origFile.active=newFile.active; 源文件:c:\Users\holcombelr\SkyDrive\Public\Projects\UHV\GalleryApp\Logic\GalleryItemFileActions.cs行:162 堆栈跟踪: [NullReferenceException:对象引用未设置为对象的实例。] GalleryApp.Logic.FileActions.UpdateFile(GalleryItemFile newFile)在c:\Users\holcombelr\SkyDrive\Public\Projects\UHV\GalleryApp\Logic\GalleryItemFileActions.cs:162中 这里有更多的信息。添加serializable让我更接近了一步,但是现在错误在下一行,我已经将serializable添加到了我的所有对象中


编辑:有人对此有答案吗?我仍然有问题。

我想我已经解决了。显然,当你有这样的子对象时,你必须在gridview的DataKeyName中包含这些对象。

你试过调试你的代码以查看哪个语句导致了错误吗?我编辑了上面的内容以显示我的调试findings。ObjectDataSource似乎没有将GalleryItemFile对象发送回UpdateFile方法。您必须检查哪个对象为null。它不是
新文件
,因为它已被访问了几行,所以它必须是
origFile.galleryItem
新文件.galleryItem
。我对实体Fra还是新的mework,但我认为它会自动加载galleryItem。上面添加了GalleryItemFile模型。我应该添加我使用的是代码优先的方法。好的,尝试公共虚拟galleryItem。(使EF能够进行延迟加载)。
public static GalleryItemFile UpdateFile(GalleryItemFile newFile)
    {
        GalleryContext _db = new GalleryContext();

        _db.Database.Log = HttpContext.Current.Response.Write; //Used for Tracing SQL
        int saved = 0;

        GalleryItemFile origFile = _db.GalleryItemFiles.FirstOrDefault(g => g.fileID == newFile.fileID);
        HttpContext.Current.Response.Write(origFile.filename);
        if (origFile == null)
        {
            //ITEM NOT FOUND
        }
        else
        {
            origFile.galleryItem.galleryItemID = newFile.galleryItem.galleryItemID;
            origFile.fileType.fileTypeID = newFile.fileType.fileTypeID;
            origFile.filename = newFile.filename;
            origFile.active = newFile.active;
            origFile.createdOn = DateTime.Now;
            origFile.createdBy = newFile.createdBy;
            origFile.resolution = newFile.resolution;

            saved = _db.SaveChanges();
        }


        if (saved > 0)
        {
            return origFile;
        }
        else { return null; }
    }
namespace GalleryApp.Models
{
[Serializable]
public class GalleryItemFile
{
    [Key]
    public int fileID { get; set; }
    public virtual GalleryItem galleryItem { get; set; }
    public virtual FileType fileType { get; set; }
    [MaxLength(75)] public string filename { get; set; }
    public bool active { get; set; }
    public DateTime? createdOn { get; set; }
    [MaxLength(150)] public string createdBy { get; set; }
    [MaxLength(50)] public string resolution { get; set; }

}
}


namespace GalleryApp.Models
{
[Serializable]
public class FileType
{
    [Key]
    public int fileTypeID { get; set; }
    [MaxLength(50)] public string fileType { get; set; }
    [MaxLength(15)] public string extension { get; set; }
    [MaxLength(250)] public string icon { get; set; }
    [MaxLength(400)] public string description { get; set; }
    [MaxLength(400)] public string warnings { get; set; }

}
}
Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 160: { Line 161: origFile.galleryItem.galleryItemID = newFile.galleryItem.galleryItemID; Line 162: origFile.fileType.fileTypeID = newFile.fileType.fileTypeID; Line 163: origFile.filename = newFile.filename; Line 164: origFile.active = newFile.active; Source File: c:\Users\holcombelr\SkyDrive\Public\Projects\UHV\GalleryApp\Logic\GalleryItemFileActions.cs Line: 162 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] GalleryApp.Logic.FileActions.UpdateFile(GalleryItemFile newFile) in c:\Users\holcombelr\SkyDrive\Public\Projects\UHV\GalleryApp\Logic\GalleryItemFileActions.cs:162