Entity framework core 实体框架核心-迁移从模型中创建所有属性,而不是所需的属性

Entity framework core 实体框架核心-迁移从模型中创建所有属性,而不是所需的属性,entity-framework-core,mapping,ef-core-2.0,columnmappings,Entity Framework Core,Mapping,Ef Core 2.0,Columnmappings,这是我在EfCore的开始(早些时候我在nHibernate和Dapper)。 我对映射有问题 我的模型是这样的: public class Document { public Guid Id {get;set;} public string Name {get;set;} public int ValueIDontWantToBeInDb {get; set;} } 我的映射: b.ToTable("documents"); b.Property(x => x.Id

这是我在EfCore的开始(早些时候我在nHibernate和Dapper)。 我对映射有问题

我的模型是这样的:

public class Document
{
   public Guid Id {get;set;}
   public string Name {get;set;}

   public int ValueIDontWantToBeInDb {get; set;}
}
我的映射:

b.ToTable("documents");
b.Property(x => x.Id).ValueGeneratedOnAdd();
b.HasKey(x => x.Id);
b.Property(x => x.Name).IsRequired();
(其中b是在IEntityTypeConfiguration实现中接收到的EntityTypeBuilder

如您所见,我从未使用ValueIDontWantToBeInDb,但EfCore一直在将其添加到表架构中。为什么会这样,以及如何使其仅添加我想要的属性

我知道有一个Ignore方法,但我必须在每个属性上的每个模型上调用它,我不想添加到模式中


我只想向EfCore展示“嘿,像这样映射这些属性”-就像在nHibernate中一样。如何做到这一点?

好的,我为此创建了一些解决方案。但我认为EfCore在标准中没有这样的解决方案,这真的是出人意料。因此,首先为所有映射创建基类。这真是一个充满魔力的地方:

abstract class BaseMap<T>: IEntityTypeConfiguration<T> where T: class, IDbItem //IDbItem is my own constraint, just an interface that has Id property
{
    EntityTypeBuilder<T> theBuilder;
    List<string> mappedPropertyNames = new List<string>();

    protected PropertyBuilder<TProperty> Map<TProperty>(Expression<Func<T, TProperty>> x) //this will be called instead of Property()
    {
        mappedPropertyNames.Add(GetPropertyName(x)); 
        return theBuilder.Property(x);
    }

    protected ReferenceNavigationBuilder<T, TRelatedEntity> HasOne<TRelatedEntity>(Expression<Func<T, TRelatedEntity>> x)
        where TRelatedEntity: class
    {
        mappedPropertyNames.Add(GetPropertyName(x));
        return theBuilder.HasOne(x);
    }

    protected CollectionNavigationBuilder<T, TRelatedEntity> HasMany<TRelatedEntity>(Expression<Func<T, IEnumerable<TRelatedEntity>>> x)
        where TRelatedEntity: class
    {
        mappedPropertyNames.Add(GetPropertyName(x));
        return theBuilder.HasMany(x);
    }

    protected PropertyBuilder<TColumnType> Map<TColumnType>(string propName)
    {
        mappedPropertyNames.Add(propName);
        return theBuilder.Property<TColumnType>(propName);
    }

    protected abstract void CreateModel(EntityTypeBuilder<T> builder);

    public void Configure(EntityTypeBuilder<T> builder)
    {
        theBuilder = builder;

        Map(x => x.Id).ValueGeneratedOnAdd();
        builder.HasKey(x => x.Id);

        CreateModel(builder);

        IgnoreUnmappedProperties(builder);

    }

    void IgnoreUnmappedProperties(EntityTypeBuilder<T> builder)
    {
        PropertyInfo[] propsInModel = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
        foreach (var prop in propsInModel)
        {
            if (!mappedPropertyNames.Contains(prop.Name))
            {
                builder.Ignore(prop.Name);
            }
        }
    }

    string GetPropertyName<TProperty>(Expression<Func<T, TProperty>> memberExpression)
    {
        MemberExpression member = null;

        switch (memberExpression.Body)
        {
            case UnaryExpression ue when ue.Operand is MemberExpression:
                member = ue.Operand as MemberExpression;
                break;

            case MemberExpression me:
                member = me;
                break;

            default:
                throw new InvalidOperationException("You should pass property to the method, for example: x => x.MyProperty");

        }

        var pInfo = member.Member as PropertyInfo;
        if (pInfo == null)
            throw new InvalidOperationException("You should pass property to the method, for example: x => x.MyProperty");

        return pInfo.Name;
    }
}
class DocumentMap : BaseMap<Document>
{
    protected override void CreateModel(EntityTypeBuilder<Document> b)
    {
        b.ToTable("documents");

        Map(x => x.Name).IsRequired();
        Map<Guid>("Owner_id").IsRequired();

        HasOne(x => x.Owner)
            .WithMany()
            .HasForeignKey("Owner_id")
            .HasConstraintName("FK_Users_Documents")
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade);

        HasMany(x => x.Periods)
            .WithOne(y => y.ParentDocument);

        HasMany(x => x.Loans)
            .WithOne(y => y.ParentDocument);
    }
}
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
    }