1

Linq から SQl にばかげた問題があります。私は C# を 3 週間しか使っていませんが、理解していると思いました。

だから、今私は自分のクラスをテストしています (エンティティとそれらのクラスを管理します) が、テストクラスの初期化を取得します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();

ここにエンティティの 1 つがあります。

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 != nullおよびaLiTm != null。はどこNullReferenceExceptionから来たのでしょうか?

あっ、そういえば。インターフェイスEntityInterfaceは空で、他のメソッドの特定されていないエンティティに名前を付けるだけです。そこにはコードはありません。

認識していただきありがとうございます。

4

1 に答える 1