PluralizingTableNameConvention

PluralizingTableNameConvention

本文介绍了一个DbSet的EF代码优先的PluralizingTableNameConvention的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅对单个表/DbSet切换此约定PluralizingTableNameConvention?据我所知,对于给定的DbContext

How do I toggle this convention PluralizingTableNameConvention for only a single table/DbSet? As far as I can tell, I can only do this to all the DbSets for a given DbContext

推荐答案

如果只有一个实体映射到未复数的表,则可以删除PluralizingTableNameConvention并手动配置该实体的表名

If you have only one entity which is mapped to a table that is not pluralizeed then you can remove the PluralizingTableNameConvention and manually configure the table name of the entity.

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Item>().ToTable("Items");
    }
 }

或者反之亦然

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Item>().ToTable("Item");
    }
 }

这篇关于一个DbSet的EF代码优先的PluralizingTableNameConvention的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:43