首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InsertOnSubmit - NullReferenceException

InsertOnSubmit - NullReferenceException
EN

Stack Overflow用户
提问于 2012-11-03 16:58:19
回答 1查看 717关注 0票数 0

我有两个模特。

AccountEntity

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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; }
}

Main

代码语言:javascript
复制
 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)

原因: by EntitySet null

请帮我把它插入。

我发现问题出在

代码语言:javascript
复制
private EntitySet<DetailsEntity> details_id { get; set; } // this is properties 

如果没有引用,这必须是新实例。

代码语言:javascript
复制
private EntitySet<DetailsEntity> details_id = new EntitySet<DetailsEntity>();
EN

回答 1

Stack Overflow用户

发布于 2012-11-06 05:14:55

尝试用以下内容更新Details表

代码语言:javascript
复制
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]

因为您需要生成数据进入其中的主键。

希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13211348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档