0

我有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

请帮我插入它。


我在

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

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

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

1 回答 1

0

尝试使用以下内容更新详细信息表

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

因为您需要在数据惰性化时生成主键。

希望这会有所帮助。

于 2012-11-06T05:14:55.857 回答