Entity Framework object reference not set when getting data from table
command = "INSERT INTO clients VALUES(" + NameTB.Text +")";
objCtx.SaveChanges();
List<Client> clients = new List<Client>();
clients = objCtx.ExecuteStoreQuery<Client>("Select * from clients").ToList();
string x = "";
for (int i = 0; i < clients.Count; i++)
x += clients[i].Name.ToString();//exception here
MessageBox.Show(x);
I just start to work with EF and have no idea how to fix this; I took the code from here
Sounds like the Name
property is null in the Client
data that is returned.
Either have a constraint in the database that this field is required or add a null check:
string x = "";
for (int i = 0; i < clients.Count; i++) {
if (!String.IsNullOrEmpty(clients[i].Name)) {
x += clients[i].Name.ToString();
}
}
Also, if you're going to work with many clients and your string is going to get big I suggest you take a look at StringBuilder.
链接地址: http://www.djcxy.com/p/76288.html上一篇: 插入一些数据时,将列从NULL更改为NOT NULL
下一篇: 从表中获取数据时未设置实体框架对象引用