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>();
此實體框架代碼是否在先? –
它似乎是LINQ到SQL(submitchanges是LINQ到SQL的功能) –