简体   繁体   English

C#Linq到SQL的SubmitChanges()错误

[英]C# Linq-to-SQL Error on submitChanges()

I have a silly problem with Linq to SQl. 我对Linq to SQl有一个愚蠢的问题。 I only work with C# for 3 weeks but I thought I understand it. 我只用C#工作了3周,但我以为我理解。

So, now I´m testing my classes (Entites and Manage classes for them) but I get in the initialization of the testclasses an NullReferenceException . 因此,现在我正在测试我的类(它们的实体和管理类),但是在测试类的初始化中得到了NullReferenceException I read that there are problems with partial classes in Linq but I have only normal classes. 我读到Linq中的局部类存在问题,但我只有普通类。 Also I have a lot of classes implementet almost the same but only in 2 (of 40) classes appears the exception. 另外,我有很多类实现几乎相同,但只有2个(共40个)类例外。 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: 这是生成表的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));");

I'm totally knocked out by this problem. 我完全被这个问题淘汰了。 The debugger can´t help me. 调试器无法帮助我。 al != null , alr != null and aLiTm != null . al != nullalr != nullaLiTm != null Where could the NullReferenceException come from? NullReferenceException可能来自哪里?

Oh, by the way. 哦,对了 The interface EntityInterface is empty and just to name some unspecific entities in other methodes. 接口EntityInterface为空,仅用于命名其他方法中的一些非特定实体。 There is no code in it. 里面没有代码。

Thanks for recognition. 感谢您的认可。

You have created a new AnimalLine instance aL but you do not have a AnimalLinesInTumorModels collection (Hence the NullReferenceException ). 您已经创建了一个新的AnimalLine实例aL但是没有AnimalLinesInTumorModels集合(因此NullReferenceException )。

You need to initialize the collection, for instace: 您需要初始化集合,以进行实例化:

aL.AnimalLinesInTumorModels = new AnimalLinesInTumorModels();

You can also assign this in the aL object initializer. 您也可以在aL对象初始化程序中分配它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM