Using MariaDb with EF Core

cation Writing

I come to write the application codes to talk to MariaDb. As always, I prefer to develop a Console application because I want to make a simple introduction to the topic. However, you can also choose to use it behind a Web API service when you try the example.

Now we need to add the necessary packages. There is a package developed by Pomelo to easily use MariaDb with Entity Framework Core . We can integrate this and other EF packages into the project using the following

Program.cs content is as follows. Do not look at the crowd. Much of it is made up of book and category adding codes.

In fact, the Entity Framework code developers are familiar with the processes that are very familiar. In the script we have books and categories that we hold about. Table attributes and MariaDb table names are specified for the Book and BookCategory types. Under normal conditions, the Primary Key field should be expressed as [Type] ID. However, we use Key to point to the key field for the BookCategory class. For some areas we also set criteria such as maximum field length and requirement with the help of attributes (Required, MaxLength attributes) There is also a relationship between the two classes. We say that there can be more than one book under one category related to one way. When this relationship is established, we also make use of ICollection <Book> type.

We have DbSetes in the DbContext-derived BeautyBooksContext class (named Books and BookCategories) and the OnConfiguring method is being truncated and the provider is being replaced with UseMySql. The parameter contains the Connection String information to be used for MariaDb.

It can be seen that two Ensure operations are called in succession in the main function. First, if the BeautyBooks database is deleted it is not a requirement. I just used it as a trial in the example. With the EnsureCreated call, they are created if the database and related objects (tables) do not exist. When the sample is first run, these databases and tables are not in the system. They will be created before this.

Several lines of data are being inserted into the Context in the following lines of code. SaveChanges also makes it possible to write attachments to the database. I have a LINQ query after adding operations. Products with a price of less than 30 units are pulled together with the categories and printed on screen in a smooth format.

Now the app can run. The output reflected in the Console window will be as follows.

On the other hand, MariaDb can be viewed from its interface. I tried the following commands.

show databases;
use BeautyBooks;
show tables;
select * from Book;
select * from Category;

The first command shows the databases. We use the use command because we want to perform actions on BeautyBooks that are created after the code works. After selecting the database, we show the tables with the show tables so that we can see that the Book and Category objects are created. We have shown the table contents with two standard Select questions (Meanwhile MariaDb’s terminal output is very nice, reminds me of the days I spent trying to fill out a table with Pascal on my DOS screen during my college years, a simple look but plain and understandable)

Log Forgetting Forgetting I

Actually, it would not be too bad if I could see what SQL statements were executed on MariaDb in the background. I of course forgot about it and realized that I was editing the article from time to time. I am now researching how to get the logs into the Console, at least by digging through Microsoft documents. Then I made the contents of BeautyBookContext as follows.

In fact, there is a very nice lesson in this piece of code. We are injecting a Log mechanism into the EF working engine. When generating EFLoggerFactory from LoggerFactory, the first parameter is ConsoleLoggerProvider. There are also different LoggerProviders, and we can also add our own LoggerProvider type here. By constructing a filter with the parameters given in the constructive method, we specify what information will be recorded at which level. We use the UseLoggerFactory operation to enable the associated LoggerFactory. As a result, when we run the code, we can see that the SQL statements in the backplane are pushed to the Console window.

As you can see, it is quite simple to use the Entity Framework and MariaDb, which is MySQL variant. Of course, one of the important issues is injecting the MariaDb provider into the EF working time. That is, the UseMySql call made in the opaque OnConfiguring function of the DbContext derived class. Therefore, I think it is very useful to examine the code content in GitHub. As a matter of fact, if we want to use our database system on the Entity Framework Core side, we will have to run similar code. So I finish a Saturday night more fun. I wish you all happy days until you see each other again.

 

The post Using MariaDb with EF Core appeared first on SQL.



from SQL http://ift.tt/2tCgzdz
via IFTTT
Using MariaDb with EF Core Using MariaDb with EF Core Reviewed by Unknown on March 11, 2018 Rating: 5

No comments:

Powered by Blogger.