NullReferenceException异常Linq中调用InsertOnSubmit时的Sql [英] NullReferenceException when calling InsertOnSubmit in Linq to Sql

查看:256
本文介绍了NullReferenceException异常Linq中调用InsertOnSubmit时的Sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想插入一个新的对象为使用LINQ to SQL我的数据库,但得到一个NullReferenceException时,我在下面的code段调用InsertOnSubmit()。我传递一个派生类称为FileUploadAudit,对象上的所有属性都设置。

I'm trying to insert a new object into my database using LINQ to SQL but get a NullReferenceException when I call InsertOnSubmit() in the code snippet below. I'm passing in a derived class called FileUploadAudit, and all properties on the object are set.

public void Save(Audit audit)
{
    try
    {                
        using (ULNDataClassesDataContext dataContext = this.Connection.GetContext())
        {
            if (audit.AuditID > 0)
            {
                throw new RepositoryException(RepositoryExceptionCode.EntityAlreadyExists, string.Format("An audit entry with ID {0} already exists and cannot be updated.", audit.AuditID));
            }

            dataContext.Audits.InsertOnSubmit(audit);
            dataContext.SubmitChanges();
        }
    }
    catch (Exception ex)
    {
        if (ObjectFactory.GetInstance<IExceptionHandler>().HandleException(ex))
        {
            throw;
        }
    }           
}

下面的堆栈跟踪:

at System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)
at XXXX.XXXX.Repository.AuditRepository.Save(Audit audit) 
C:\XXXX\AuditRepository.cs:line 25"

我已经加入到这样的审计类:

I've added to the Audit class like this:

public partial class Audit
{ 
    public Audit(string message, ULNComponent component) : this()
    {
        this.Message = message;
        this.DateTimeRecorded = DateTime.Now;
        this.SetComponent(component);
        this.ServerName = Environment.MachineName;
    }           
    public bool IsError { get; set; }     
    public void SetComponent(ULNComponent component)
    {
        this.Component = Enum.GetName(typeof(ULNComponent), component);
    }
}

和派生FileUploadAudit看起来是这样的:

And the derived FileUploadAudit looks like this:

public class FileUploadAudit : Audit
{ 
    public FileUploadAudit(string message, ULNComponent component, Guid fileGuid, string originalFilename, string physicalFilename, HttpPostedFileBase postedFile)
        : base(message, component)
    {
        this.FileGuid = fileGuid;
        this.OriginalFilename = originalFilename;
        this.PhysicalFileName = physicalFilename;
        this.PostedFile = postedFile;
        this.ValidationErrors = new List<string>();
    }
    public Guid FileGuid { get; set; }
    public string OriginalFilename { get; set; }
    public string PhysicalFileName { get; set; }
    public HttpPostedFileBase PostedFile { get; set; }
    public IList<string> ValidationErrors { get; set; }
}

任何想法的问题是什么?我能找到的矿最近的问题是这里但我的部分审计类调用的参数的构造函数产生code,我仍然得到这个问题。

Any ideas what the problem is? The closest question I could find to mine is here but my partial Audit class is calling the parameterless constructor in the generated code, and I still get the problem.

更新:

此问题只当我通过在派生类FileUploadAudit,审计类工作正常发生。审计类生成一个的LINQ to SQL类,并没有属性映射到数据库字段在派生类中。

This problem only occurs when I pass in the derived FileUploadAudit class, the Audit class works fine. The Audit class is generated as a linq to sql class and there are no Properties mapped to database fields in the derived class.

推荐答案

做了一些研究之后,我得出的结论,有没有简单的方法来解决直接提交继承的对象的这个问题。这是可能的地图继承层次的,但这已经无关,与你pretend。你只是想提交基础 并不在乎,如果它实际上是一个派生

After doing a little research, I came to the conclusion that there's no easy way to solve this problem of submitting the inherited object directly. It's possible to Map Inheritance Hierarchies but this has nothing to do with what you pretend. You just want to submit the base class and don't care if it's actually a derived class.

如果你不想使用部分类来附加属性或行为添加到您的表类,如<一个建议href="http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception">thread你提到的,我会做一个简单的克隆的方法作为一种解决方法:

If you don't want to use a partial class to add additional properties or behavior to your table class, as suggested in the thread you mentioned, I would do a simple Clone method as a workaround:

    public partial class Audit
    {
        // ...
        public Audit Concrete()
        {
            if (this.GetType().Equals(typeof(Audit)))
                return this;
            else
            {
                Audit audit = new Audit();
                // clone properties
                return audit;
            }
        }
    }

//...
dataContext.Audits.InsertOnSubmit(audit.Concrete());

这篇关于NullReferenceException异常Linq中调用InsertOnSubmit时的Sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆