Use Database metadata to generate comments for EntityFramwork entities

Source: Internet
Author: User
Using Database metadata to generate comments for EntityFramwork entities as we all know, EntityFramwork automatically generates entities without comments. Img1 is correct. I am using the database first mode in various application modes of EntityFramwork. This article does not discuss which application mode of EntityFramwork is better. Instead, it discusses EntityFramwork.

Using Database metadata to generate comments for the Entity Framwork Entity is well known that there is no comment on the Entity automatically generated by Entity Framwork. Img1 is correct. I am using the database first mode in various application modes of Entity Framwork. This article does not discuss the question about which application mode of Entity Framwork is better. Instead, it discusses Entity Framwork.

Use Database metadata to generate comments for Entity Framwork entities

As we all know, there is no comment on the Entity automatically generated by Entity Framwork.

Img1

Yes, I am using the database first mode in various application modes of Entity Framwork. This article does not discuss the question about which application mode of Entity Framwork is better. Instead, it discusses the issue that the T4 template provided by Entity Framwork does not generate Entity annotations.

The metadata describing the architecture is usually written in the architecture database. For example

Img2

I don't care where SqlServer stores the metadata. However, it seems that this metadata is a key-Value Pair structure. This type of metadata is called Extended Properties.

Img3

Generally, databases provide interfaces for data operations in the form of database hobbies. What is the interface for operating the metadata of the Extended Properties category here? Is the following system metadata function and three system stored procedures:

Img4

Img5

Img6

Img7

These four interfaces are used to perform the CRUD operation on Extended Properties class metadata. Unfortunately, Entity Framwork's object data model does not contain such attributes. Although this type of metadata has nothing to do With ORM, it is advantageous for our code, if we can add this type of metadata to the entity class code in the form of annotations.

To confirm that the Entity Framwork object data model does not contain such metadata, we create an Entity Framwork project in the database-first mode, and update metadata from the database after a database is created.

Img8

Enable SqlServer Profiler before clicking "Update Model From Database"

Img9

By monitoring the SQL code of Entity Framwork to update metadata, it can be found that Entity Framwork does not query metadata of Extended Properties category. As mentioned above, this kind of metadata has nothing to do With ORM, So Entity Framwork does not need to query such metadata at all. However, we want to use the Extended Properties metadata in the database to automatically add comments to the entity code. This is the case for us. It is difficult to use the metadata on the object data model to add comments to the object code because Ef does not check such metadata at all.

Then, we build our own model. By monitoring Entity Framwrok and updating metadata SQL code in the database, we have learned about SQL Server metadata data access. The study found that the following four types of data are queried for generating the Entity Data Model metadata Entity Framwork:

Img10

The four types of metadata correspond to the SqlServer table, view, table column, and view column respectively. Of course, these four types of metadata are now represented by four database views which have been encapsulated by me. We use these four views as the "interface" to provide metadata ". That's right, it's an "interface". Who has specified that an interface must be a structure defined by the interface keyword in C? Interfaces are contracts, and these four views are our database contracts. Now every database I create has implemented these interfaces. They all have these four views. These four views are created by referring to the SQL statement for obtaining metadata by referring to Entity Framwork. These SQL queries are too complex. In particular, it is difficult to query the metadata of table columns and view columns. Here is only one table View:

Img11

With the above four views, we can add three system stored procedures sys. sp_addextendedproperty, sys. sp_updateextendedproperty, sys. sp_dropextendedproperty and a system metadata function: fn_Listentendedproperty. If this knowledge is familiar to us or noticed, most people can have a general solution in their minds. But as a good final, this article will continue. Let's talk about the architecture and coding below.

We have found that we have been talking about databases all the time. We can see that the database concept is very important. So I want to abstract it and create a Db class:

Img12

There is nothing to say about the Db class, but its GetConnection method is worth mentioning. Let's take a look at the class diagram above:

Img13

The GetConnection method of the Db class is used to construct a database connection by entrusting ADO. NET, while DbProviderFactory is an instance of the dbProviderFactory type through ADO.. NET DbProviderFactories. getFactory (this. providerName) Static Method passed in the ProviderName string to build

Img14

Of course, apart from the Db class, we also need to model the metadata of the table, table column, view, and view column categories. However, the four of them are not brilliant:

Img15

Do you still remember the four view interfaces on the database mentioned above? These four object models map the four views (relational models) in the database ). Do Db-type objects not need to be persisted? Of course, it must be persisted to the Database table in the Database:

Img16

Similarly, the Database table has nothing to offer. As mentioned in the previous figure, the Database is connected to the "pilot Database". The Database where the Hong Kong server and Database table are located is the pilot Database.

The model is created. to simplify the problem, the above Db model, DbTable model, DbView model, DbTableColumn model, and DbViewColumn model are used as data access models. Now we define a set of metadata access interfaces based on these four models:

Img17

Currently, it makes sense to use several methods in red circles. These methods are used to add, delete, modify, and delete the database metadata of the Extended Properties category. They are all implemented by calling a very smelly private method:

Img18

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.