I am getting an exception
System.InvalidOperationException : No backing field could be found for property 'ApartmentId' of entity type 'Address' and the property does not have a getter.
This is my Apartment
class:
public class Apartment
{
public Apartment(Address address)
{
Address = address;
}
private Apartment()
{
}
public int Id { get; private set; }
public Address Address { get; private set; }
}
This is my Address
value object class:
public class Address : IEquatable<Address>
{
private Address()
{
}
public Address(string streetNumber, string streetName, string city, string state, string zipCode)
{
StreetNumber = streetNumber;
StreetName = streetName;
City = city;
State = state;
ZipCode = zipCode;
}
public string StreetNumber { get; private set; }
public string StreetName { get; private set; }
public string City { get; private set; }
public string State { get; private set; }
public string ZipCode { get; private set; }
public bool Equals(Address other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return String.Equals(StreetNumber, other.StreetNumber, StringComparison.OrdinalIgnoreCase) &&
String.Equals(StreetName, other.StreetName, StringComparison.OrdinalIgnoreCase) &&
String.Equals(City, other.City, StringComparison.OrdinalIgnoreCase) &&
String.Equals(State, other.State, StringComparison.OrdinalIgnoreCase) &&
String.Equals(ZipCode, other.ZipCode, StringComparison.OrdinalIgnoreCase);
}
}
In my entity configuration I use builder.OwnsOne(a => a.Address);
. In my repository I make this call:
public async Task<Apartment> GetByAddressAsync(Address address)
{
return await _context.Apartments.FirstOrDefaultAsync(a => a.Address.Equals(address));
}
And it produces the exception above. Any ideas? I don't know why it says I have an 'ApartmentId' in my Address
value object.