1

I have a silly problem with Linq to SQl. I only work with C# for 3 weeks but I thought I understand it.

So, now I´m testing my classes (Entites and Manage classes for them) but I get in the initialization of the testclasses an NullReferenceException. I read that there are problems with partial classes in Linq but I have only normal classes. Also I have a lot of classes implementet almost the same but only in 2 (of 40) classes appears the exception. Here is a part of the test initialization:

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

Here you will find one of the Entities:

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

Here the other side:

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; }
        }

       ...
    }
}

Here another entity without any problems. It is very similar:

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

They all will look almost the same. And here is the SQL Code to generate the table:

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

I'm totally knocked out by this problem. The debugger can´t help me. al != null, alr != null and aLiTm != null. Where could the NullReferenceException come from?

Oh, by the way. The interface EntityInterface is empty and just to name some unspecific entities in other methodes. There is no code in it.

Thanks for recognition.

4

1 回答 1

1

You have created a new AnimalLine instance aL but you do not have a AnimalLinesInTumorModels collection (Hence the NullReferenceException).

You need to initialize the collection, for instace:

aL.AnimalLinesInTumorModels = new AnimalLinesInTumorModels();

You can also assign this in the aL object initializer.

于 2013-05-03T10:50:26.193 回答