these are my beginnings with EfCore (earlier I was in nHibernate and Dapper). I have a problem with mappings.
My model looks like that:
public class Document
{
public Guid Id {get;set;}
public string Name {get;set;}
public int ValueIDontWantToBeInDb {get; set;}
}
My mappings:
b.ToTable("documents");
b.Property(x => x.Id).ValueGeneratedOnAdd();
b.HasKey(x => x.Id);
b.Property(x => x.Name).IsRequired();
(where b is EntityTypeBuilder received in IEntityTypeConfiguration implementation.
As you can see, I never use ValueIDontWantToBeInDb, but EfCore keeps adding this to table schema. Why is it so and what to do to make it add only those properties that I want?
I know there is a Ignore method. But then I would have to call it on every model on every property that I do not want to be added to schema.
I just want to show to EfCore - "Hey, map these properties like so" - just like in nHibernate. How to do this?
Ok, I create some solution for this. But I think that it's really outreagous that EfCore doesn't have such a solution in standard. So, first create base class for all your mappings. This is really the place with all the magic:
Now example of a class that derives from this and creates real object map:
Notice that instead of using methods from EntityTypeBuilder, I use methods derived from base class. Frankly speaking I even don't have to pass EntityTypeBuilder object here.
And all is called in DbContext like this: