C# 在Linq to Sql中调用InsertOnSubmit时出现NullReferenceException

C# 在Linq to Sql中调用InsertOnSubmit时出现NullReferenceException,c#,.net,linq-to-sql,nullreferenceexception,C#,.net,Linq To Sql,Nullreferenceexception,我试图使用LINQ to SQL将一个新对象插入到数据库中,但在下面的代码段中调用InsertOnSubmit()时得到一个NullReferenceException。我传入了一个名为FileUploadAudit的派生类,对象上的所有属性都已设置 public void Save(Audit audit) { try { using (ULNDataClassesDataContext dataContext = this.Co

我试图使用LINQ to SQL将一个新对象插入到数据库中,但在下面的代码段中调用InsertOnSubmit()时得到一个NullReferenceException。我传入了一个名为FileUploadAudit的派生类,对象上的所有属性都已设置

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;
        }
    }           
}
我在Audit类中添加了如下内容:

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);
    }
}
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; }
}
派生的FileUploadAudit如下所示:

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);
    }
}
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; }
}
公共类FileUploadAudit:Audit { 公共文件上载审核(字符串消息、ULNComponent组件、Guid文件Guid、字符串原始文件名、字符串物理文件名、HttpPostedFileBase postedFile) :基本(消息、组件) { this.FileGuid=FileGuid; this.OriginalFilename=OriginalFilename; this.PhysicalFileName=PhysicalFileName; this.PostedFile=PostedFile; this.ValidationErrors=新列表(); } 公共Guid文件Guid{get;set;} 公共字符串OriginalFilename{get;set;} 公共字符串物理文件名{get;set;} 公共HttpPostedFileBase PostedFile{get;set;} 公共IList验证错误{get;set;} } 你知道问题出在哪里吗?我能找到的最接近我的问题是,我的部分审计类正在调用生成代码中的无参数构造函数,我仍然遇到了这个问题

更新:


这个问题只有在传入派生的FileUploadAudit类时才会发生,Audit类工作正常。Audit类是作为linq to sql类生成的,并且派生类中没有映射到数据库字段的属性。

在做了一些研究之后,我得出结论,直接提交继承的对象并没有简单的方法来解决这个问题。这是可能的,但这与你的伪装无关。您只想提交
基本类
,而不关心它是否是派生的

如果您不想使用
分部类
向表类添加其他属性或行为(如您提到的中所建议),我将使用一种简单的克隆方法作为解决方法:

    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());

尝试创建一个只具有属性ID和typeDiscriminator的基本抽象类;然后创建从基继承的具体类,并最终创建从上一个继承的其他类。 不要将属性包含在已存在于base one中的派生类中

示例:假设一个名为BasePeoples的基础抽象类具有属性ID和typeDiscriminator,我们有一个Person类型的类继承自该基础,还有另外两个Fisherman和Driver类型的类继承自表单Person

所以你可以做这样的事情

using (DatabaseDataContext db = new DatabaseDataContext())
        {
            Person p = new Person();
            p.FirstName = "Dario";
            p.LastName = "Iacampo";

            Fisherman f = new Fisherman();
            f.FirstName = "San";
            f.LastName = "Pei";
            f.CollectedFishes = 10;
            f.FishingLicenceNumber = "abc";

            Driver d = new Driver();
            d.FirstName = "Michael";
            d.LastName = "Shumaker";
            d.CarMake = "Ferrari";
            d.DrivingLicenceNumber = "123abc";

            db.BasePeoples.InsertOnSubmit(f);
            db.BasePeoples.InsertOnSubmit(d);
            db.SubmitChanges();

        }

如果您只是想预先配置东西,那么继承的替代方案可能是:

partial class MyLinqClass {
    string Text = "Default";

    public MyLinqClass AsOne() {
        Text = "One";
        ...
        return this;
    }
}

var x = new MyLinqClass().AsOne();
context.InsertOnSubmit(x); // x is type MyLinqClass

你能发布异常的堆栈跟踪吗?添加了堆栈跟踪,但对Hanks没有多大帮助,这就是我得出的结论。