Say now I have a table in a database for properties:
properties
id
num_bedrooms
num_bathrooms
num_garages
latitude
longitude
street_1
street_2
street_3
suburb
city
region
country
Now I would like to CRUD to it from my ASP .Net Core Web API using EF Core. So I create a model for it, but I would like to create a separate class for the address:
public class Address { [Column("latitude")] public double Latitude { get; set; } [Column("longitude")] public double Longitude { get; set; } [Column("street_1")] public string Street1 { get; set; } [Column("street_2")] public string Street2 { get; set; } [Column("street_3")] public string Street3 { get; set; } [Column("suburb")] public string Suburb { get; set; } [Column("city")] public string City { get; set; } [Column("region")] public string Region { get; set; } [Column("country")] public string Country { get; set; } } public class Property { [Column("num_bedrooms")] public int NumBedrooms { get; set; } [Column("num_bathrooms")] public int NumBathrooms { get; set; } [Column("num_garages")] public int NumGarages { get; set; } public Address StreetAddress { get; set; } }
Is it possible to do something like that? When I try to execute a read against the database in my API controller like this:
public DbSet<InspectionsData.Models.Property> Properties { get; set; }
// GET: api/Properties Authorize] [HttpGet] public async Task<IActionResult> GetProperty() { return Ok(_context.Properties); }
I get this error:
"The entity type 'Address' requires a primary key to be defined."
Now, I can add an "Id" to the Address class, but it wouldn't really map to anything in the database... Am I doing this right?