【问题标题】:ORM and prefixes for the tables表的 ORM 和前缀
【发布时间】:2015-11-13 13:11:42
【问题描述】:

任务这个,在数据库中注册客户时我们需要创建一组具有唯一前缀的表,一般可以用EF实现吗?

【问题讨论】:

  • 我真的认为最好的管理方法是使用模式。

标签: c# asp.net-mvc entity-framework orm


【解决方案1】:

我没有对此进行测试,但它应该可以工作,或者至少是一个很好的起点。首先,您需要告诉上下文您将使用的前缀,我会在构造函数中执行此操作。然后在 OnModelCreating 覆盖中,为表名使用前缀:

public class MyContext : DbContext
{
    private readonly string _prefix;

    public MyContext(string prefix)
    {
        _prefix = prefix;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Types()
            .Configure(entity => entity.ToTable(_prefix + entity.ClrType.Name));
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2017-08-04
    相关资源
    最近更新 更多