如何使用Entity Framework Core配置“身份”列? [英] How to configure an Identity column using Entity Framework Core?

查看:130
本文介绍了如何使用Entity Framework Core配置“身份”列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Entity Framework Core中创建自动增量身份列?

How do I create an Auto increment identity column in Entity Framework Core?

很明显,我可以使用流利的EF6 API来做到这一点。

Obviously I can do it using fluent API for EF6 for example.

推荐答案

由于EF7文档很少,所以我们知道很多我们必须从源代码或单元测试中收集。根据EF7源代码中的以下两个单元测试...

Since there is very little EF7 documentation, much of what we know we have to glean from the source or unit tests. According to the following two unit tests in the EF7 source...


此处此处

您将为Identity配置以下属性:

You would configure a property for Identity like this:

b.Property(e => e.Id).ForSqlServer().UseIdentity();

您将为Sequences配置一个属性,如下所示:

And you would configure a property for Sequences like this:

ForSqlServer().UseSequence();

网址由于首次提出了aspnet-core重组,因此已更改,方法也已更改。

The urls have changed due to the aspnet-core reorg, and the methods have also changed since this was first asked.

此处此处

if (_useSequence) 
{
    b.Property(e => e.Identifier).ForSqlServerUseSequenceHiLo();
} 
else 
{
    b.Property(e => e.Identifier).UseSqlServerIdentityColumn();
}

这些网址可能会再次更改(这就是为什么我包含相关代码的原因),但仅查看该URL并转到该网站并弄清楚新的URL是何等的简单。

It's possible these urls might change again (which is why I include the relevant code), but it's ridiculously easy to just look at the url and go to the site and figure out what the new url is.

真的,我的回答的重点是您可以通过查看GitHub上源代码中的单元测试来自己弄清楚这些东西。

Really, the whole point of my answer is that you can figure this stuff out yourself just by going and looking at the unit tests in the source code on GitHub. You shouldn't need someone to spoon feed it to you.

编辑:更新了指向版本2.1的链接(仍然适用于1.1和2.0)

Updated links to version 2.1 (still works for 1.1 and 2.0 as well)

这篇关于如何使用Entity Framework Core配置“身份”列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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