实体框架核心流利模型生成器的关键和属性 [英] EntityFramework Core Fluent Model Builder Key and Property

查看:454
本文介绍了实体框架核心流利模型生成器的关键和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实在实体框架6我将在一个语句中生成一个密钥和属性数据库:

Ok so in entity framework 6 I would have had a key and property database generation in one statement:

modelBuilder.Entity<Function>()
                .HasKey(x => x.Id)
                .Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

在实体框架核心(7)中,这不起作用:

In entity framework core (7) this does not work:

modelBuilder.Entity<Function>()
                .HasKey(x => x.Id)
                .Property(x => x.Id)
                .ValueGeneratedNever();

错误:'KeyBuilder'不包含'属性'和无扩展名的定义方法'属性'接受'KeyBuilder'类型的第一个参数:

这必须是两个单独的语句,如下所示或在那里有一种方法可以像你一样在EF6中使用?

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Function>()
                    .HasKey(x => x.Id);

    modelBuilder.Entity<Function>()
                    .Property(x => x.Id)
                    .ValueGeneratedNever();
}


推荐答案

是的,他们是分开的EF核心。

Yes, they are separate in EF Core.

原因是因为EF6方法允许您指定PK列,没有更多,因此返回 EntityTypeConfiguration< TEntityType> (与从 Entity< ...> 调用获取的目标相同),因此您可以继续流畅地配置实体类型。

The reason is because EF6 method allows you to specify the PK columns and nothing more, hence is returning EntityTypeConfiguration<TEntityType> (the same as the target obtained from Entity<...> call) and that's why you can continue fluently configuring the entity type.

然而,EF Core方法返回一个名为 KeyBuilder 的不同类型,它允许您进一步配置PK,如 HasName 用于关系约束名称,或特定数据库属性,如 ForSqlServerHasName ForSqlServerIsClustered 等等。

However the EF Core method returns a different type called KeyBuilder which allows you to further configure the PK, like HasName for the relational constraint name, or specific database attributes like ForSqlServerHasName and ForSqlServerIsClustered etc.

这篇关于实体框架核心流利模型生成器的关键和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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