简体   繁体   English

在EF Core中设置属性约定?

[英]Set Property Conventions in EF Core?

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). 在NHibernate中,他们有一个简洁的约定概念,你可以说,如果一个实体有一个Name属性,并且它是一个字符串,你可以对它应用某些db行为(例如,将它设置为非null,max长度x,如果您可以确定所有名称都是唯一的,则可能是唯一的)。

It would be a small class, you'd add it to your instance factory, and bam! 这将是一个小班,你将它添加到你的实例工厂,并且bam! The db model would build accordingly. db模型将相应地构建。

Is there a comparable mechanism in EF Core? 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. 我似乎找不到任何东西,每个房产的每个实体的流畅配置数量都非常繁琐。

In EF Core the built-in conventions are used to create an initial model, which you can override or modify in OnModelCreating. 在EF Core中,内置约定用于创建初始模型,您可以在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. 这可以很好地代替EF6的自定义约定(至少到目前为止)自定义约定没有使其脱离积压。 See https://github.com/aspnet/EntityFrameworkCore/issues/214 for status and some examples. 有关状态和一些示例,请参阅https://github.com/aspnet/EntityFrameworkCore/issues/214 It's also a pretty common pattern to read a custom attribute in OnModelCreating to drive (or override) your entity configuration. 在OnModelCreating中读取自定义属性以驱动(或覆盖)实体配置也是一种非常常见的模式。

So your example: 你的榜样如此:

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). 如果实体具有Name属性,并且它是一个字符串,则可以对其应用某些db行为(例如,将其设置为非null,max length x,如果您确定所有名称都是唯一的,则可能是唯一的)。

would be something like: 会是这样的:

    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);
    }

Or if you wanted to force the datatypes for all DataTime columns, something like: 或者,如果要强制所有DataTime列的数据类型,请执行以下操作:

    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)";

    }

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

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