如何使用 Entity Framework Core 配置标识列?

编程入门 行业动态 更新时间:2024-10-08 19:44:25
本文介绍了如何使用 Entity Framework Core 配置标识列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在 Entity Framework Core 中创建自动递增标识列?

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

显然,我可以使用 EF6 的 fluent 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();

你会像这样为序列配置一个属性:

And you would configure a property for Sequences like this:

ForSqlServer().UseSequence();

由于 aspnet-core 重组,urls 发生了变化,自从第一次询问以来,方法也发生了变化.

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

这些网址可能会再次更改(这就是我包含相关代码的原因),但是只需查看网址并转到网站并找出新网址是非常容易的.

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 配置标识列?

本文发布于:2023-10-26 06:39:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   标识   Framework   Entity   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!