EF architecture ~ Data initialization in the CodeFirst model, efcodefirst

Source: Internet
Author: User

EF architecture ~ Data initialization in the CodeFirst model, efcodefirst

Back to directory

Why did I come?

In traditional large-scale system design, database modeling is an earlier stage than development. First, databases, Then ORM models, and finally programs are developed, this kind of model changes after the emergence of EF, and may be replaced by code first in the future, because your relational database does not have to be fixed. When you deploy a website, no one wants to create a lot of SQL statements first. Anyone wants to automatically generate a database after it runs on IIS. Whether it is sqlserver, orcale, or mysql, it would be nice if it is automatically generated! In fact, this development model has already entered our world, especially when deploying third-party servers, this code first makes you feel more convenient, you do not need to create data tables one by one. For a third-party server such as the Hong Kong cloud, normal users do not support SQL commands, and tables can only be created one by one.

Data Initialization

When your database is created, the data table information can be initialized at the same time. This is what we want to talk about today. It is divided into two uncles. First, enable this initialization function in config, second, create your own Initializer class to implement the DropCreateDatabaseIfModelChanges <YourContext> generic method. Take a look at the code.

1. Enable configuration. Modify entityFramework in web. config as follows:

<entityFramework>    <contexts>      <context type="Lind.DDD.Manager.ManagerContext, Lind.DDD.Manager">        <databaseInitializer type="Lind.DDD.Manager.ManagerInitializer, Lind.DDD.Manager" />      </context>    </contexts>    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">      <parameters>        <parameter value="v12.0" />      </parameters>    </defaultConnectionFactory>  </entityFramework>

2. Write initialization code

/// <Summary> // database initialization // </summary> public class ManagerInitializer: DropCreateDatabaseIfModelChanges <ManagerContext> {protected override void Seed (ManagerContext context) {try {# region department table var department = new webdocumments {About = "", DepartmentName = "", DeptLevel = 0, Operator = "admin", ParentID = null, sortNumber = 0, Status = 1, UpdateDate = DateTime. now,}; context. webscripts. add (department); context. saveChanges (); # endregion # region menu table var menu = new WebManageMenus {About = "", LinkUrl = "", MenuLevel = 1, MenuName = "root ", operator = "admin", ParentID = null, SortNumber = 0, Status = 1, UpdateDate = DateTime. now,}; context. webManageMenus. add (menu); context. saveChanges (); # endregion base. seed (context) ;}catch (Exception) {throw ;}}}

Thank you for reading this article. I hope this article will help you!

Back to directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.