submitChanges()上的C#Linq-to-SQL错误

时间:2013-05-03 10:47:50

标签: c# linq-to-sql nullreferenceexception submitchanges

我对Linq到SQl有一个愚蠢的问题。我只与C#合作了3个星期,但我认为我理解它。

所以,现在我正在测试我的类(Entites和Manage类),但我在测试类的初始化NullReferenceException。我读到Linq中的部分类存在问题,但我只有普通的类。此外,我有很多类实现几乎相同,但只有2(40个)类出现异常。以下是测试初始化​​的一部分:

     AnimalLine aL = new AnimalLine { ShortName = "Bl6", FullName = "C57Bl6N", Background = "C57Bl6N", SecureEntity = se, Phenotype = "Coat Color = Black", Species = sp };
   ...
     dataContext.GetTable<TumorModel>().InsertOnSubmit(tm);
     AnimalLineInTumorModel aLiTm = new AnimalLineInTumorModel { AnimalLineRole = alr, OntogeneticStage = os, AnimalLine = aL, TumorModel = tm };

    aL.AnimalLinesInTumorModels.Add(aLiTm); // <- if i outcomment this two rows, i have no exception
     alr.AnimalLineInTumorModels.Add(aLiTm); // <- 

     os.AnimalLineInTumorModel.Add(aLiTm);
     tm.AnimalLinesInTumorModels.Add(aLiTm);
     dataContext.GetTable<AnimalLineInTumorModel>().InsertOnSubmit(aLiTm);
 ...
    dataContext.SubmitChanges();

在这里您可以找到其中一个实体:

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Tumormodelle.BusinessTierObjects.TumorModels;

namespace Tumormodelle.BusinessTierObjects.Animals
{
    [Table(Name = "AnimalLineRole")]
    public class AnimalLineRole : EntityInterface
    {
        // PrimaryKey
        [Column(Name = "AnimalLineRole_ID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int AnimalLineRoleId { get; set; }

        //  normal Column
        [Column(Name = "AnimalLineRole_Name", CanBeNull = false)]
        public string Name { get; set; }

        // ForeignKey to AnimalLineInTumorModel (1:M)
        private EntitySet<AnimalLineInTumorModel> _AnimalLineInTumorModels = new EntitySet<AnimalLineInTumorModel>();
        [Association(Name = "FK_AnimalLineInTumorModel_AnimalLineRole", IsForeignKey = true, Storage = "_AnimalLineInTumorModels", ThisKey = "AnimalLineRoleId", OtherKey = "AnimalLineRoleId")]
        public ICollection<AnimalLineInTumorModel> AnimalLineInTumorModels
        {
            get { return _AnimalLineInTumorModels; }
            set { _AnimalLineInTumorModels.Assign(value); }
        }
    }
}

这是另一面:

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Tumormodelle.BusinessTierObjects.Animals;

namespace Tumormodelle.BusinessTierObjects.TumorModels
{
    [Table(Name = "AnimalLineInTumorModel")]
    public class AnimalLineInTumorModel : EntityInterface
    {
        // PrimaryKey
        [Column(Name = "AnimalLineInTumorModel_ID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int AnimalLineInTumorModelId { get; set; }

        // ForeignKey to AnimalLineRole (M:1)
        [Column(Name = "AnimalLineInTumorModel_FK_Role", CanBeNull=false)]
        private int? AnimalLineRoleId;
        private EntityRef<AnimalLineRole> _AnimalLineRole = new EntityRef<AnimalLineRole>();
        [Association(Name = "FK_AnimalLineInTumorModel_AnimalLineRole", IsForeignKey = true, Storage = "_AnimalLineRole", ThisKey = "AnimalLineRoleId", OtherKey = "AnimalLineRoleId")]
        public AnimalLineRole AnimalLineRole
        {
            get { return _AnimalLineRole.Entity; }
            set { _AnimalLineRole.Entity = value; }
        }

        // ForeignKey to AnimalLine (M:1)
        [Column(Name = "AnimalLineInTumorModel_FK_AnimalLine", CanBeNull = false)]
        private int? AnimalLineId;
        private EntityRef<AnimalLine> _AnimalLine = new EntityRef<AnimalLine>();
        [Association(Name = "FK_AnimalLineInTumorModel_AnimalLine", IsForeignKey = true, Storage = "_AnimalLine", ThisKey = "AnimalLineId", OtherKey = "AnimalLineId")]
        public AnimalLine AnimalLine
        {
            get { return _AnimalLine.Entity; }
            set { _AnimalLine.Entity = value; }
        }

        // ForeignKey to OntogeneticStage (M:1)
        [Column(Name = "AnimalLineInTumorModel_FK_OntogeneticStage", CanBeNull = false)]
        private int? OntogeneticStageId;
        private EntityRef<OntogeneticStage> _OntogeneticStage = new EntityRef<OntogeneticStage>();
        [Association(Name = "FK_AnimalLineInTumorModel_OntogeneticStage", IsForeignKey = true, Storage = "_OntogeneticStage", ThisKey = "OntogeneticStageId", OtherKey = "OntogeneticStageId")]
        public OntogeneticStage OntogeneticStage
        {
            get { return _OntogeneticStage.Entity; }
            set { _OntogeneticStage.Entity = value; }
        }

       ...
    }
}

这里另一个实体没有任何问题。它非常相似:

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Tumormodelle.BusinessTierObjects.TumorModels;

namespace Tumormodelle.BusinessTierObjects.Animals
{
    [Table(Name="OntogeneticStage")]
    public class OntogeneticStage : EntityInterface
    {
        // Primärschlüssel
        [Column(Name = "OntogeneticStage_ID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int OntogeneticStageId { get; set; }

        // Normale Tabelleneinträge
        [Column(Name = "OntogeneticStage_Name", CanBeNull = false)]
        public string Name { get; set; }

        // Fremdschlüssel zu AnimalLineInTumorModel (1:M)
        private EntitySet<AnimalLineInTumorModel> _AnimalLineInTumorModel = new EntitySet<AnimalLineInTumorModel>();
        [Association(Name = "FK_AnimalLineInTumorModel_OntogeneticStage", Storage = "_AnimalLineInTumorModel", OtherKey = "OntogeneticStageId", ThisKey = "OntogeneticStageId")]
        public ICollection<AnimalLineInTumorModel> AnimalLineInTumorModel
        {
            get { return _AnimalLineInTumorModel; }
            set { _AnimalLineInTumorModel.Assign(value); }
        }
    }
}

它们看起来几乎一样。这是生成表的SQL代码:

dataContext.ExecuteCommand("CREATE TABLE [dbo].[AnimalLineRole] 
([AnimalLineRole_ID] INT IDENTITY (1, 1) ,
[AnimalLineRole_AutoDate] DATETIME CONSTRAINT 
[DF_AnimalLineRole_AnimalLineRole_AutoDate] DEFAULT (getdate()) , 
[AnimalLineRole_Timestamp] ROWVERSION ,
[AnimalLineRole_Comments] NVARCHAR (255) NULL,
[AnimalLineRole_Name] NVARCHAR (255) NULL,
CONSTRAINT [PK_AnimalLineRole] PRIMARY KEY CLUSTERED ([AnimalLineRole_ID] ASC));");

我完全被这个问题击败了。调试器无法帮助我。 al != nullalr != nullaLiTm != nullNullReferenceException来自哪里?

哦,顺便说一下。接口EntityInterface为空,只是为了在其他方法中命名一些非特定实体。它中没有代码。

感谢您的认可。

1 个答案:

答案 0 :(得分:1)

您已创建新的AnimalLine实例aL,但您没有AnimalLinesInTumorModels个集合(因此NullReferenceException)。

您需要初始化集合,例如:

aL.AnimalLinesInTumorModels = new AnimalLinesInTumorModels();

您也可以在aL对象初始值设定项中指定它。