簡體   English   中英

InsertOnSubmit-NullReferenceException

[英]InsertOnSubmit - NullReferenceException

我有2個模型。

帳戶實體

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

詳細實體

[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 null

請幫我把它插入。


我在發現問題

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

如果沒有引用,則必須是新實例

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

嘗試使用以下內容更新詳細信息表

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

因為您需要在將數據插入時生成主鍵。

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM