在EF Core中设置属性约定? [英] Set Property Conventions in EF Core?

查看:225
本文介绍了在EF Core中设置属性约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NHibernate中,他们有一个约定的漂亮小概念,您可以说,如果实体具有Name属性,并且它是一个字符串,则可以对其应用某些db行为(例如,对其进行设置)不能为null,最大长度x,如果可以确定所有名称都是唯一的,则可以唯一).

In NHibernate they had a nifty little concept of a convention, where you could say, if an entity had a Name property, and it was a string, you could apply certain db behavior to it (for example, set it to not null, max length x, maybe unique if you could be certain all names would be unique).

这将是一个小类,您将其添加到您的实例工厂中,ba! db模型将相应地构建.

It would be a small class, you'd add it to your instance factory, and bam! The db model would build accordingly.

EF Core中是否有类似的机制?我似乎找不到任何东西,而且对于每个实体,每个属性的流利配置数量都非常可笑.

Is there a comparable mechanism in EF Core? I can't seem to find any and the ridiculous number of fluent configurations for every entity for every property is quite tedious.

推荐答案

在EF Core中,内置约定用于创建初始模型,您可以在OnModelCreating中覆盖或修改该初始模型.

In EF Core the built-in conventions are used to create an initial model, which you can override or modify in OnModelCreating.

您可以简单地遍历您的实体,并且属性将覆盖约定.这足以代替EF6的自定义约定,(至少到目前为止)自定义约定还没有解决.有关状态,请参见 https://github.com/aspnet/EntityFrameworkCore/issues/214 和一些例子.在OnModelCreating中读取自定义属性以驱动(或覆盖)您的实体配置也是一种很常见的模式.

You can simply iterate over your entities and properties override the conventions. This works well enough in place of custom conventions from EF6 that (at least so far) custom conventions haven't made it off the backlog. See https://github.com/aspnet/EntityFrameworkCore/issues/214 for status and some examples. It's also a pretty common pattern to read a custom attribute in OnModelCreating to drive (or override) your entity configuration.

所以你的例子:

如果实体具有Name属性,并且它是一个字符串,则可以对其应用某些db行为(例如,将其设置为not null,最大长度x,如果可以确定所有名称都可以是唯一的,则可以是唯一的唯一).

if an entity had a Name property, and it was a string, you could apply certain db behavior to it (for example, set it to not null, max length x, maybe unique if you could be certain all names would be unique).

应该是这样的:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var props = from e in modelBuilder.Model.GetEntityTypes()
                    from p in e.GetProperties()
                    where p.Name == "Name"
                       && p.PropertyInfo.PropertyType == typeof(string)
                    select p;

        foreach (var p in props)
        {
            p.SetMaxLength(200);
            p.IsNullable = false;
            p.DeclaringEntityType.AddKey(p);
        }

        base.OnModelCreating(modelBuilder);
    }

或者如果您想强制所有DataTime列的数据类型,则类似:

Or if you wanted to force the datatypes for all DataTime columns, something like:

    var dateProps = from e in modelBuilder.Model.GetEntityTypes()
                from p in e.GetProperties()
                where p.PropertyInfo.PropertyType == typeof(DateTime)
                select p;
    foreach (var prop in dateProps)
    {
        prop.Relational().ColumnType = "datetime2(4)";

    }

这篇关于在EF Core中设置属性约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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