2013-05-03 53 views
1

Linq SQl我有一个愚蠢的问题。我只用C#工作了3周,但我认为我理解它。所以,现在我正在测试我的类(Entites和管理它们的类),但我得到了初始化测试类NullReferenceException。我读过Linq中存在部分类的问题,但我只有普通的类。此外,我有很多类实现几乎相同,但只有2(40)类出现例外。下面是测试初始化​​的一部分:C#Linq到SQL错误submitChanges()

 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 != null,alr != nullaLiTm != nullNullReferenceException从哪里来?

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

感谢您的认可。

+0

Linq to SQL不推荐用于新项目。对于所有意图和目的,它已被弃用Linq EntityFramework – Aron 2013-05-03 10:50:49

+0

我可以看到你有其他关系。由于'AnimalLine'不在这里发布,请检查它的任何关系是否是**必需**。如果是这样,那么你应该提供相关的实体,因为相关的实体不能为空 – noobob 2013-05-03 11:16:20

+0

@noobob:他们只是表明,他们没有问题,看起来类似问题类。例如,我编辑了另一个运行的实体。 – Jastol 2013-05-03 11:21:17

回答

1

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

您需要初始化集合,instace:

aL.AnimalLinesInTumorModels = new AnimalLinesInTumorModels(); 

,您还可以在aL对象初始化分配此。

+0

如果id这样做,就说:“没有创建抽象类或接口的实例”。 另外,我不会在任何其他类(实体)中做到这一点,他们都会运行。 – Jastol 2013-05-03 11:06:19

+0

另外我在调试器中检查过它。 ICollections是definetly!= null。 – Jastol 2013-05-03 11:25:42