禁用EF6中的所有约定 [英] Disable all conventions in EF6

查看:71
本文介绍了禁用EF6中的所有约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,EF6内置约定已经存在并且可能还有更多问题。通过调试,我看到代码首先EF6启用了所有可用的默认约定。

Today I have had, and possibly still have more problems with EF6 built-in conventions. By debugging, I saw that code first EF6 enables all the default conventions available.

由于我已经使用mapper类生成了美观的模型映射,所以我只是没有想要约定以某种方式改变我的数据模型。

Since I have generated a good-looking model mapping with mapper classes, I just don't want conventions to someway alter my data model.

具体来说,我无法运行简单的 SELECT 查询因为FK列正确映射为 linkedentity 时,伪列 Extent1.LinkedEntity_id 进入了SQL我没有理由在数据库中使用其他名称。

In the specific, I can't run a simple SELECT query because of a spurious column Extent1.LinkedEntity_id that gets into the SQL, when the FK column is correctly mapped as linkedentity and I have no reason to use a different name in my database.

为清楚起见:此代码优先模型是从数据库优先进行反向工程的。数据库规则,我无法更改其设计。

For clearness: this code-first model is reverse engineered from a DB-first. Database rules and I can't change its design.

问题:如何立即禁用所有约定?似乎我在 DbModelBuilder.Conventions

Question: how do I disable all conventions at once? Seems I can't find a .Clear method in DbModelBuilder.Conventions

推荐答案

没有原生方法可以将其删除。但是,可以使用反射来做。找到所有约定,然后在它们上调用删除。这样的事情应该有所帮助:

There is no native way of removing them. But, it could be possible to do using reflection. Find all Conventions and then call Remove on them. Something like this should help:

    private void RemoveAllConventions(DbModelBuilder modelBuilder)
    {

        var conventions = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes().Where(t => t.IsClass && t.GetInterface("IConvention") != null));


        var remove = typeof(ConventionsConfiguration).GetMethods().Where(m => m.Name == "Remove" && m.ContainsGenericParameters).First();
        foreach (var item in conventions)
        {
            try
            {
                remove.MakeGenericMethod(item).Invoke(modelBuilder.Conventions, null);
            }
            catch (Exception)
            {
            }
        }
    }

,然后在OnModelCreating中:

And then in your OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        RemoveAllConventions(modelBuilder);
    }

请随时重复此概念并报告您的发现。

Feel free to iterate more on this concept and report back your findings.

这篇关于禁用EF6中的所有约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆