EF Core Migrations doesn't pick up all properties

.net c# entity-framework entity-framework-core

Question

When using EF Core Migrations the tooling isn't picking up all my properties, only the "Id" and some (not all) of the FK's (navigation properties). It also seems to understand the "Table Per Hierarchy" very well - as it has set up a discriminator where I wanted it to, but the migration file itself doesn't reflect the properties I have in my Model namespace (see github code).

My DbContext are located here: https://github.com/aredfox/TheConference/blob/master/InfoBooth/TheConference.InfoBooth.Data/InfoBoothContext.cs My Model classes here: https://github.com/aredfox/TheConference/tree/master/InfoBooth/TheConference.InfoBooth.Core/Model

You can look at the generated "Initial Migration" at [ https://github.com/aredfox/TheConference/blob/master/InfoBooth/TheConference.InfoBooth.Data/Migrations/20170604125116_Initial.cs ]. There you'll see that it detected all the Id properties (defined in the base class [Entity][1]), but none of the other properties I of my Model classes.

I used the approach of adding a Console App to run migrations (otherwise it won't work - see Julie Lerman's tips on getting started with EF Core). Thus when running migrations I do it this way: dotnet ef --startup-project ../../TheConference.Infrastructure.ConsoleApp migrations add Initial.

Do I have to use annotations or modelBuilder or a form of EntityTypeConfiguration in EFCore to let EF know what I want to take?.

1
2
6/11/2018 6:02:47 PM

Accepted Answer

All of your class properties are like this

public string Title { get; }

i.e. read only auto properties.

EF Core does not support (map) such properties. In order to get them mapped, you need to provide property setters

public string Title { get; set; }

The access level does not really matter - private, protected or internal will work. The only requirement is to have a setter.

EF Core also allows mapping properties and using backing fields, but all that requires fluent API configuration and also most likely will not work with get only auto properties because they are backed with readonly fields.

9
6/4/2017 2:06:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow