[英]Creating database first and code first in Entity Framework
Please I have a question and I have searched extensively on Google but couldn't get concrete answers.请我有一个问题,我在谷歌上进行了广泛的搜索,但无法得到具体的答案。
I have a project.我有一个项目。 I already used the Identity framework and by this, it generated the database using code first.我已经使用了 Identity 框架,因此它首先使用代码生成了数据库。
I intend to use database-first for subsequent tables.我打算将数据库优先用于后续表。
My questions are below:我的问题如下:
Can I generate the database first such that it uses same DbContext
as the code first entities?我可以先生成数据库,使其使用与代码优先实体相同的DbContext
吗?
Will they have separate connection strings?他们会有单独的连接字符串吗?
Do I have to continue using code-first, or can the two approaches be combined in a project?我必须继续使用代码优先,还是可以将这两种方法结合在一个项目中?
I am actually new to Entity Framework.我实际上是实体框架的新手。
Thanks谢谢
Yes, you can.是的你可以。 you can generate the database schema by code first, then turn to database first, when you use database first to generate the DbContext, it will follow the rules that you generated by code first.你可以先用代码生成数据库模式,然后再转向数据库,当你先用数据库生成DbContext时,它会遵循你先用代码生成的规则。
No, I guess that you have to do it manually.不,我想你必须手动完成。
It's not a good practice to use both code first and db first in a project, so I would recommend code-first only.在项目中同时使用 code first 和 db first 不是一个好习惯,所以我建议只使用 code-first。
For further information, you can take a look at this有关更多信息,您可以查看此
- Can I generate the database first such that it uses same
DbContext
as the code first entities?我可以先生成数据库,使其使用与代码优先实体相同的DbContext
吗?
No you can't, assuming you're using ADO.NET Entity Data Model
/ .edmx
to import your database tables you generated into your project.不,您不能,假设您使用ADO.NET Entity Data Model
/ .edmx
将您生成的数据库表导入项目。
The reason is, when you use Code First
, your connection string in the web.config
will look something like the following:原因是,当您使用Code First
时, web.config
中的连接字符串将如下所示:
<connectionStrings>
<add name="YOUR_IDENTITY_CONTEXT_NAME" providerName="System.Data.SqlClient"
connectionString="Data Source=xxx; Initial Catalog=xxx; User Id=xxx; Password=xxx;" />
</connectionStrings>
But when you use .edmx
to import tables into models/classes into your project, it won't see your existing connection string you had with Code First
.但是,当您使用.edmx
将表导入到项目中的模型/类中时,它不会看到您使用Code First
拥有的现有连接字符串。 Instead, you will have to create a new connection string, which will look like the following:相反,您必须创建一个新的连接字符串,如下所示:
<connectionStrings>
<add name="YOUR_DATABASE_FIRST_CONTEXT_NAME" providerName="System.Data.EntityClient"
connectionString="metadata=res://*/xxx|res://*/xxx|res://*/xxx;provider=System...." />
</connectionStrings>
- Will they have separate connection strings?他们会有单独的连接字符串吗?
Yes, they will.是他们会。
- Do I have to continue using code-first, or can the two approaches be combined in a project?我必须继续使用代码优先,还是可以将这两种方法结合在一个项目中?
It would be great if you can use Code First
all the way through, but if you can't, there are other ways you can mix Code First
and Database First
approach:如果您可以一直使用Code First
,那就太好了,但如果您不能,还有其他方法可以混合使用Code First
和Database First
方法:
Just use 2 separate connection strings只需使用 2 个单独的连接字符串
I typically name the Identity DbContext AppIdentityDbContext
, while the data context generated by .edmx
just AppDbContext
.我通常将 Identity DbContext 命名为AppIdentityDbContext
,而.edmx
生成的数据上下文只是AppDbContext
。
Use different ORMs other than Entity Framework
使用Entity Framework
以外的其他 ORM
You don't have to stick with one ORM in a project.您不必在项目中坚持使用 ORM。 You can use Entity Framework
for identity stuff, and use something else, such as Dapper , for reading.您可以将Entity Framework
用于身份信息,并使用其他东西,例如Dapper来阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.