简体   繁体   English

实体框架+ Fluent轻量级配置仅适用于实体类型

[英]Entity Framework + Fluent Lightweight configuration for entity types only

I am trying to configure table names in model so that each table belongs to schema named by containing namespace with this code: 我正在尝试在模型中配置表名,以便每个表都属于通过使用以下代码包含名称空间来命名的架构:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    if (modelBuilder == null) throw new ArgumentNullException(nameof(modelBuilder));

    foreach (var creator in ModelCreators)
    {
        creator.OnModelCreating(modelBuilder);
    }
    modelBuilder.Types().Configure(c => c.ToTable(PrefixNamespace(c.ClrType)));
}

private string PrefixNamespace(Type type)
{
    var result = $"{type.Namespace.Split('.').Last()}.{type.Name}";
    return result;
}

The problem is that applying this configuration affects ComplexTypes as well, which are then configured as entities. 问题在于,应用此配置也会影响ComplexType,然后将它们配置为实体。 How to apply this configuration to entities only? 如何将此配置仅应用于实体? The modelBuilder.Entity<TEntity>(TEntity entity) can configure only one entity at the time, but not all entity types are known during build time, some will be discovered from .dlls. modelBuilder.Entity<TEntity>(TEntity entity)当时只能配置一个实体,但是在构建期间并不是所有实体类型都是已知的,有些将从.dll中发现。

I was thinking about something like 我在想类似的东西

modelBuilder.Types().Where(t => IsNotComplexType(t)).Configure(...)

but I don't know how to implement IsNotComplexType form available objects. 但是我不知道如何实现IsNotComplexType形式的可用对象。

我得出的结论是,必须通过DbModelBuilder.ComplexType<TComplexType>()显式设置复杂类型,因此在应用ToTable(string tableName)方法后,它不会变成实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM