标识符不能是空字符串

时间:2013-01-03 19:44:59

标签: c# .net linq-to-sql

我正在使用Linq To Sql来保存一个包含许多子实体的实体,并且只是想知道是否有人遇到此错误:The identifier cannot be an empty string

我只是想知道是否有人遇到过这个问题,你做了什么来解决这个问题?也许我会很幸运,你的一个解决方案也可以。

这可能没什么用,但这里是代码失败的地方

public void SaveJob(object sender, CancelEventArgs e)
{

   Job = JobEditorUtilities.CreateNewJob(jobid, SettingsManager.OpsMgrSettings.Region.Trim(), _db);
    try
    {
        var canSaveJob = CanSaveJob();
        if (canSaveJob)
        {
            JobEditorUtilities.PrepareJobForSaving(Job);
            if (newjob)
                _db.JobNumbers.InsertOnSubmit(Job);

            var a = _db.GetChangeSet();
            _db.SubmitChanges();   // fails here
            RenameJobNumberIfNecessary();

        }
        else
        {
            e.Cancel = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


    public static JobNumber CreateNewJob(int jobid, string region, DataAccessDataContext dc)
    {
        var job = new JobNumber();
        job.JobID = jobid;

        InitializeSubContractor(dc, job);
        job.GWDRunSummaryDatas = new GWDRunSummaryData();
        job.CorrectionsApplieds = new CorrectionsApplied();
        job.MonitorInfos = new MonitorInfo();
        InitializeGWDHeader(dc, job);
        job.Rpt_Details = new Rpt_Detail();
        job.RunningModes = new RunningMode();
        job.SafetyIssuesTotals = new SafetyIssuesTotals();
        job.SafetyIssueTotals = new SafetyIssueTotals();
        switch (region)
        {
            case "NA":
                InitializeNA_Header(dc, job);
                job.NA_WRdetails = new NA_WRdetail();
                InitializeNA_BillingCompany(dc, job);
                job.NA_RigDetails = new NA_RigDetail();
                job.NA_Holedetails = new NA_Holedetail();
                job.NA_Salesmans = new NA_Salesman();
                InitializeNA_WL_Company(dc, job);
                break;
            case "EU":
                InitializeEAC_Header(dc, job);
                break;
        }



        return job;
    }
   private static void InitializeNA_WL_Company(DataAccessDataContext dc, JobNumber job)
    {
        job.NA_WL_Companies = new NA_WL_Company();
        job.NA_WL_Companies.Resource =
            dc.Resources.FirstOrDefault(r => r.Refno == job.NA_WL_Companies.WL_companyid);
    }

    private static void InitializeGWDHeader(DataAccessDataContext dc, JobNumber job)
    {
        job.GWD_Header2s = new GWD_Header2();
        job.GWD_Header2s.GWDDirComp2 =
            dc.GWDDirComp2s.FirstOrDefault(g => g.ID == job.GWD_Header2s.MWD_Company);

    }


    private static void InitializeNA_BillingCompany(DataAccessDataContext dc, JobNumber job)
    {
        job.NA_BillingCompanies = new NA_BillingCompany();
        job.NA_BillingCompanies.Resource =
            dc.Resources.FirstOrDefault(r => r.Refno == job.NA_BillingCompanies.Billingcompanyid);
    }
    private static void InitializeSubContractor(DataAccessDataContext dc, JobNumber job)
    {
        job.Subcontractors = new Subcontractor();
        job.Subcontractors.Resource =
            dc.Resources.FirstOrDefault(r => r.Refno == job.Subcontractors.SubcontractorID);
    }

    private static void InitializeNA_Header(DataAccessDataContext dc, JobNumber job)
    {
        job.NA_Headers = new NA_Header();
        job.NA_Headers.RigType1 = dc.RigTypes.FirstOrDefault(r => r.ID == job.NA_Headers.RigType);
        job.NA_Headers.Well_Locn1 = dc.Well_Locns.FirstOrDefault(w => w.ID == job.NA_Headers.Well_Locn);
    }

    private static void InitializeEAC_Header(DataAccessDataContext dc, JobNumber job)
    {
        job.EAC_Headers = new EAC_Header();

    }

2 个答案:

答案 0 :(得分:0)

我最终重新开始并根据需要初始化子实体,而不是一次初始化它们。这似乎解决了问题

答案 1 :(得分:0)

此问题是由您的某个类字段映射具有空白的“Source”属性引起的。如果您使用的是LINQ to SQL设计器,请单击每个类的每个字段,并确保“Source”属性不为空。如果要手动创建类,请确保在“名称”字段中没有带有空字符串的ColumnAttributes。