[英]Retrieve nullable object using EF CORE
I am a beginner, and while testing some code, I can't seem to understand how to do this properly..我是初学者,在测试一些代码时,我似乎无法理解如何正确执行此操作。
1st: I have a City class:第一:我有一个城市 class:
public class City
{
public City()
{
ZipCode = "";
Name = "";
}
public int Id { get; set; }
public string ZipCode { get; set; }
public string Name { get; set; }
}
2nd: I have a Contact class that uses a nullable City class ( in case the user does not know the city ):第二:我有一个联系人 class,它使用可为空的城市 class(以防用户不知道城市):
public class Contact
{
public Contact()
{
Name = "";
Line1 = "";
Line2 = "";
CityId = null;
City = new City();
}
public int Id { get; set; }
public string Name { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
private int? _CityId;
public int? CityId
{
get { return _CityId; }
set { _CityId = value < 1 ? null : value; }
}
private City _City;
public City City
{
get { return _City; }
set { _City = _CityId == null ? null : value; }
}
}
The problem I encounter is when I retrieve a record that has stored a null City ( when retrieving a record and its City is not null, everything will work fine ).我遇到的问题是当我检索存储了 null City的记录时(当检索记录且其 City 不是 null 时,一切正常)。 My.Select() statement looks like this:
My.Select() 语句如下所示:
var result = await _context.Contact
.Where(w => w.Id == id)
.Include(q => q.City)
.Select(s => new Contact
{
// Entity
Id = s.Id,
// Model
Line1 = s.Line1,
Line2 = s.Line2,
CityId = s.CityId,
City = new City // I am retrieving this so that I can get the inner data as well
{
// Entity
Id = s.City.Id,
// Model
ZipCode = s.City.ZipCode,
Name = s.City.Name,
}
}).FirstOrDefaultAsync();
The output is fine for records that does not have a null City, but if user retrieves a record with null City, it throws the following error : output 适用于没有 null 城市的记录,但如果用户检索带有 null 城市的记录,则会抛出以下错误:
Nullable object must have a value.可为空的 object 必须有一个值。
Can anybody please teach me how to do this properly?谁能教我如何正确地做到这一点? Thank you in advance!
先感谢您!
You don't need to create a new Entity using Select, you are getting the error because if s.City is null s.City.Id doesn't exists.您不需要使用 Select 创建新实体,您会收到错误消息,因为如果 s.City 为 null,则 s.City.Id 不存在。 Insteat search it directly using
Insteat直接使用搜索
var result = await _context.Contact
.Include(q => q.City)
.FirstOrDefaultAsync(x => x.Id == id);
Why you use Select and using private property for the city ?为什么你用Select ,用私有财产给城市?
Contact Class:联系电话 Class:
public class Contact
{
public Contact()
{
Name = "";
Line1 = "";
Line2 = "";
CityId = null;
City = new City();
}
public int Id { get; set; }
public string Name { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public int? CityId;
public City City
}
Your Select is the same as the entity class, and you don't need to use it.你的Select和实体class是一样的,你不用用。
var result = await _context.Contact
.Include(q => q.City)
.FirstOrDefaultAsync(f => f.Id == id);
Thanks for the answers everyone!谢谢大家的解答!
I found that the culprit is this line of code ( thanks to your answers and explanations ):我发现罪魁祸首是这行代码(感谢您的回答和解释):
Id = s.City.Id Id = s.City.Id
That is because Id here is an int ( not nullable since this is an Identity in MSSQL ) but when CityId ( and also City object ) stores a null in the database, s.City.Id will also contain null in the.Select().这是因为这里的Id是一个int (不可为 null,因为这是 MSSQL 中的一个身份)但是当CityId (以及 City object )在数据库中存储null时, s.City.Id也将在 .Select() 中包含null .
The Id = s.City.Id in the above case fails because Id ( not nullable ) is being forced to receive a null ( contained in the s.City.Id ).上述情况下的Id = s.City.Id失败,因为Id (不可为空)被迫接收null (包含在 s.City.Id 中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.