C# Linq2Sql context.SubmitChanges()生成;对象引用未设置为对象的实例。”;错误

C# Linq2Sql context.SubmitChanges()生成;对象引用未设置为对象的实例。”;错误,c#,wcf,linq-to-sql,C#,Wcf,Linq To Sql,我有一个web服务,它使用Linq2Sql将记录插入数据库 web服务方法如下所示: public class WS_StreamCrossing : IStreamCrossing { public string SendStreamCrossingSurveys(List<Survey> mySurveyList) { if (mySurveyList == null) { throw new Argumen

我有一个web服务,它使用Linq2Sql将记录插入数据库

web服务方法如下所示:

public class WS_StreamCrossing : IStreamCrossing
{
    public string SendStreamCrossingSurveys(List<Survey> mySurveyList)
    {
        if (mySurveyList == null)
        {
            throw new ArgumentNullException("mySurveyList");
        }

        string result = "Failed";


        using (StreamCrossingDataContext myObjectContext = GetDBContext())
        {
            foreach (Survey myPassedSurvey in mySurveyList)
            {
                // Check for existing Survey with matching data (PRNo, MP, FWVersion, CulvertID, SurveyDate)
                Survey myMatchingSurvey = myObjectContext.Surveys.FirstOrDefault(x => x.PRNo == myPassedSurvey.PRNo && x.MP == myPassedSurvey.MP && x.FWVersion == myPassedSurvey.FWVersion && x.CulvertID == myPassedSurvey.CulvertID && x.SurveyDate == myPassedSurvey.SurveyDate);
                if (myMatchingSurvey != null)
                {

                    myMatchingSurvey.AdjacentLandowner = myPassedSurvey.AdjacentLandowner;
                    myMatchingSurvey.ApproachLengthLeft = myPassedSurvey.ApproachLengthLeft;
                    myMatchingSurvey.ApproachLengthRight = myPassedSurvey.ApproachLengthRight;
                    myMatchingSurvey.ApproachSlopeLeft = myPassedSurvey.ApproachSlopeLeft;
                    myMatchingSurvey.ApproachSlopeRight = myPassedSurvey.ApproachSlopeRight;
                    myMatchingSurvey.BackwaterComments = myPassedSurvey.BackwaterComments;
                    myMatchingSurvey.CalculatedPassability = myPassedSurvey.CalculatedPassability;
                    myMatchingSurvey.County = myPassedSurvey.County;
                    myMatchingSurvey.CrossingType = myPassedSurvey.CrossingType;
                    myMatchingSurvey.CrushedLocation = myPassedSurvey.CrushedLocation;
                    myMatchingSurvey.CrushedPercent = myPassedSurvey.CrushedPercent;
                    //myMatchingSurvey.CulvertID = myPassedSurvey.CulvertID;
                    myMatchingSurvey.DefinedPassability = myPassedSurvey.DefinedPassability;
                    myMatchingSurvey.ErosionExtent = myPassedSurvey.ErosionExtent;
                    myMatchingSurvey.ErosionNotes = myPassedSurvey.ErosionNotes;
                    myMatchingSurvey.FillDepthDownStream = myPassedSurvey.FillDepthDownStream;
                    myMatchingSurvey.FillDepthUpStream = myPassedSurvey.FillDepthUpStream;
                    myMatchingSurvey.FutureComments = myPassedSurvey.FutureComments;
                    myMatchingSurvey.FutureVisit = myPassedSurvey.FutureVisit;
                    //myMatchingSurvey.FWVersion = myPassedSurvey.FWVersion;
                    myMatchingSurvey.GeneralCondition = myPassedSurvey.GeneralCondition;
                    myMatchingSurvey.HeadChangeComments = myPassedSurvey.HeadChangeComments;
                    myMatchingSurvey.InletStructure = myPassedSurvey.InletStructure;
                    myMatchingSurvey.IsBackwatered = myPassedSurvey.IsBackwatered;
                    myMatchingSurvey.IsErosionCorrectable = myPassedSurvey.IsErosionCorrectable;
                    myMatchingSurvey.IsErosionPresent = myPassedSurvey.IsErosionPresent;
                    myMatchingSurvey.IsHeadChanged = myPassedSurvey.IsHeadChanged;
                    myMatchingSurvey.IsOvertopping = myPassedSurvey.IsOvertopping;
                    myMatchingSurvey.IsPerched = myPassedSurvey.IsPerched;
                    myMatchingSurvey.IsPrioritySite = myPassedSurvey.IsPrioritySite;
                    myMatchingSurvey.IsRustedThrough = myPassedSurvey.IsRustedThrough;
                    myMatchingSurvey.IsScourPoolPresent = myPassedSurvey.IsScourPoolPresent;
                    myMatchingSurvey.IsSubstrateEntireLength = myPassedSurvey.IsSubstrateEntireLength;
                    myMatchingSurvey.IsUpstreamPondPresent = myPassedSurvey.IsUpstreamPondPresent;
                    myMatchingSurvey.Latitude = myPassedSurvey.Latitude;
                    myMatchingSurvey.LocationComments = myPassedSurvey.LocationComments;
                    myMatchingSurvey.LocationOfLowPoint = myPassedSurvey.LocationOfLowPoint;
                    myMatchingSurvey.Longitude = myPassedSurvey.Longitude;
                    //myMatchingSurvey.MP = myPassedSurvey.MP;
                    myMatchingSurvey.NonNativeInvasiveSpecies = myPassedSurvey.NonNativeInvasiveSpecies;
                    myMatchingSurvey.Observers = myPassedSurvey.Observers;
                    myMatchingSurvey.OutletStructure = myPassedSurvey.OutletStructure;
                    myMatchingSurvey.OutletType = myPassedSurvey.OutletType;
                    myMatchingSurvey.OvertoppingComments = myPassedSurvey.OvertoppingComments;
                    myMatchingSurvey.PassabilityComments = myPassedSurvey.PassabilityComments;
                    myMatchingSurvey.PerchHeight = myPassedSurvey.PerchHeight;
                    myMatchingSurvey.PhotoDownstream = myPassedSurvey.PhotoDownstream;
                    myMatchingSurvey.PhotoInlet = myPassedSurvey.PhotoInlet;
                    myMatchingSurvey.PhotoOutlet = myPassedSurvey.PhotoOutlet;
                    myMatchingSurvey.PhotoRoadApproachLeft = myPassedSurvey.PhotoRoadApproachLeft;
                    myMatchingSurvey.PhotoRoadApproachRight = myPassedSurvey.PhotoRoadApproachRight;
                    myMatchingSurvey.PhotoUpstream = myPassedSurvey.PhotoUpstream;
                    myMatchingSurvey.PluggedLocation = myPassedSurvey.PluggedLocation;
                    myMatchingSurvey.PluggedPercent = myPassedSurvey.PluggedPercent;
                    myMatchingSurvey.PriorityComments = myPassedSurvey.PriorityComments;
                    myMatchingSurvey.PriorityReason = myPassedSurvey.PriorityReason;
                    //myMatchingSurvey.PRNo = myPassedSurvey.PRNo;
                    myMatchingSurvey.PtID = myPassedSurvey.PtID;
                    myMatchingSurvey.RefMiles = myPassedSurvey.RefMiles;
                    myMatchingSurvey.RiffleBankfullWidth = myPassedSurvey.RiffleBankfullWidth;
                    myMatchingSurvey.RiffleMeasuredWith = myPassedSurvey.RiffleMeasuredWith;
                    myMatchingSurvey.RiffleSubstrate = myPassedSurvey.RiffleSubstrate;
                    myMatchingSurvey.RiffleWaterDepth = myPassedSurvey.RiffleWaterDepth;
                    myMatchingSurvey.RiffleWaterVelocity = myPassedSurvey.RiffleWaterVelocity;
                    myMatchingSurvey.RiffleWettedWidth = myPassedSurvey.RiffleWettedWidth;
                    myMatchingSurvey.RoadCondition = myPassedSurvey.RoadCondition;
                    myMatchingSurvey.RoadName = myPassedSurvey.RoadName;
                    myMatchingSurvey.RoadSurface = myPassedSurvey.RoadSurface;
                    myMatchingSurvey.RoadWidth = myPassedSurvey.RoadWidth;
                    myMatchingSurvey.RunOffPath = myPassedSurvey.RunOffPath;
                    myMatchingSurvey.ScourPoolDepth = myPassedSurvey.ScourPoolDepth;
                    myMatchingSurvey.ScourPoolLength = myPassedSurvey.ScourPoolLength;
                    myMatchingSurvey.ScourPoolWidth = myPassedSurvey.ScourPoolWidth;
                    myMatchingSurvey.SiteID = myPassedSurvey.SiteID;
                    myMatchingSurvey.SlopeDownStream = myPassedSurvey.SlopeDownStream;
                    myMatchingSurvey.SlopeUpStream = myPassedSurvey.SlopeUpStream;
                    myMatchingSurvey.SpeciesObserved = myPassedSurvey.SpeciesObserved;
                    myMatchingSurvey.StreamFlow = myPassedSurvey.StreamFlow;
                    myMatchingSurvey.StructureEmbeddedDepthInlet = myPassedSurvey.StructureEmbeddedDepthInlet;
                    myMatchingSurvey.StructureEmbeddedDepthOutlet = myPassedSurvey.StructureEmbeddedDepthOutlet;
                    myMatchingSurvey.StructureInterior = myPassedSurvey.StructureInterior;
                    myMatchingSurvey.StructureShape = myPassedSurvey.StructureShape;
                    myMatchingSurvey.StructureWaterDepthInlet = myPassedSurvey.StructureWaterDepthInlet;
                    myMatchingSurvey.StructureWaterDepthOutlet = myPassedSurvey.StructureWaterDepthOutlet;
                    myMatchingSurvey.StructureWaterVelocityInlet = myPassedSurvey.StructureWaterVelocityInlet;
                    myMatchingSurvey.StructureWaterVelocityMeasured = myPassedSurvey.StructureWaterVelocityMeasured;
                    myMatchingSurvey.StructureWaterVelocityMeasuredWith = myPassedSurvey.StructureWaterVelocityMeasuredWith;
                    myMatchingSurvey.StructureWaterVelocityOutlet = myPassedSurvey.StructureWaterVelocityOutlet;
                    myMatchingSurvey.SubstrateInStructure = myPassedSurvey.SubstrateInStructure;
                    //myMatchingSurvey.SurveyDate = myPassedSurvey.SurveyDate;
                    myMatchingSurvey.UpstreamPondLength = myPassedSurvey.UpstreamPondLength;
                    myMatchingSurvey.UpstreamPondWidth = myPassedSurvey.UpstreamPondWidth;
                    myMatchingSurvey.VegetationDitchLeft = myPassedSurvey.VegetationDitchLeft;
                    myMatchingSurvey.VegetationDitchRight = myPassedSurvey.VegetationDitchRight;
                    myMatchingSurvey.Waterway = myPassedSurvey.Waterway;


                    // Delete all Erosions
                    myObjectContext.Erosions.DeleteAllOnSubmit(myMatchingSurvey.Erosions);

                    // Assign new Erosions
                    myMatchingSurvey.Erosions = myPassedSurvey.Erosions;

                    // Delete all Spans
                    myObjectContext.Spans.DeleteAllOnSubmit(myMatchingSurvey.Spans);

                    // Assign new Spans
                    myMatchingSurvey.Spans = myPassedSurvey.Spans;

                }
                else
                {
                    myObjectContext.Surveys.InsertOnSubmit(myPassedSurvey);
                }
            }

            myObjectContext.SubmitChanges();
            result = "Success";

        }

        return result;
    }
有什么想法吗

在回答问题时,这里有更多的数据

这是
GetDBContext()
方法:

private StreamCrossingDataContext GetDBContext()
{
    SERVICE.StreamCrossingDataContext db = new SERVICE.StreamCrossingDataContext();

    DataLoadOptions options = new DataLoadOptions();

    options.LoadWith<SERVICE.Survey>(x => x.Erosions);
    options.LoadWith<SERVICE.Survey>(x => x.Spans);

    db.LoadOptions = options;

    return db;
}
private StreamCrossingDataContext GetDBContext()
{
SERVICE.StreamCrossingDataContext db=new SERVICE.StreamCrossingDataContext();
DataLoadOptions=new DataLoadOptions();
options.LoadWith(x=>x.Erosions);
options.LoadWith(x=>x.span);
db.LoadOptions=选项;
返回分贝;
}

GetDBContext()方法返回什么?如果不知道属性的所有数据类型,很难说,但是您可能在一个赋值中将非null引用设置为null吗?@kundansinghchoushan我已经用GetDBContext()更新了这个问题方法实现。@user957902这是作为SOAP web服务设置的,并且是我直接从Linq2Sql DMBL文件传入实际“Survey”对象的对象。我不相信它会让我将非null值类型设置为null。但我已经看过了,就我所见,任何值类型不允许为null的字段,它都会放入默认值(例如,0代表int,“”代表string,等等)。
private StreamCrossingDataContext GetDBContext()
{
    SERVICE.StreamCrossingDataContext db = new SERVICE.StreamCrossingDataContext();

    DataLoadOptions options = new DataLoadOptions();

    options.LoadWith<SERVICE.Survey>(x => x.Erosions);
    options.LoadWith<SERVICE.Survey>(x => x.Spans);

    db.LoadOptions = options;

    return db;
}