如何使用Entity Framework Core配置“身份”列?

编程入门 行业动态 更新时间:2024-10-08 22:16:40
本文介绍了如何使用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配置“身份”列?

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

发布评论

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

>www.elefans.com

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