2012-11-03 29 views
0

我有2个模型。InsertOnSubmit - NullReferenceException

AccountEntity

[Table(Name = "Account")] 
    public class AccountEntity 
    { 
     [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
     public int id { get; set; } 
     [Column(CanBeNull = false, Name = "email")] 
     public string email { get; set; } 
     [Column(CanBeNull = false, Name = "pwd")] 
     public string pwd { get; set; } 
     [Column(CanBeNull = false, Name = "row_guid")] 
     public Guid guid { get; set; } 

     private EntitySet<DetailsEntity> details_id { get; set; } 
     [Association(Storage = "details_id", OtherKey = "id", ThisKey = "id")] 
     public ICollection<DetailsEntity> detailsCollection { get; set; } 
    } 

DetailsEntity

[Table(Name = "Details")] 

    public class DetailsEntity 
    { 
    public DetailsEntity(AccountEntity a) { 
     this.Account = a; 
    } 

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "int")] 
    public int id { get; set; } 

    private EntityRef<AccountEntity> _account = new EntityRef<AccountEntity>(); 
    [Association(IsForeignKey = true, Storage = "_account", ThisKey = "id")] 
    public AccountEntity Account { get; set; } 
} 

主要

using (Database db = new Database()) 
{ 
    AccountEntity a = new AccountEntity(); 
    a.email = "hahaha"; 
    a.pwd = "13212312"; 
    a.guid = Guid.NewGuid(); 
    db.Account.InsertOnSubmit(a); 
    db.SubmitChanges(); 
} 

有关系AccountEntity <- DetailsEntity (1-n)

当我试图插入抛出异常的记录(NullReferenceException

原因:由EntitySet

请帮我做插入。在

private EntitySet<DetailsEntity> details_id { get; set; } // this is properties 


我发现问题这必须是新的实例,如果没有裁判

private EntitySet<DetailsEntity> details_id = new EntitySet<DetailsEntity>(); 
+0

此实体框架代码是否在先? –

+0

它似乎是LINQ到SQL(submitchanges是LINQ到SQL的功能) –

回答

0

尝试用以下

[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 

更新明细表您需要生成Pri玛丽把数据的惯性关键入其中。

希望这会有所帮助。