Nov 23, 2021
4 Views

Primary data of EntityFramework for .internet developer

Written by

IntroI want to share basic knowledge about EntityFramework (EF) which should be enough to implement most of trivial tasks working with relational database. This articles should help young developers to understand EF better and avoid some mistakes which I did on my way.PreparationFirstly lets prepare a database. It reflects most trivial case with post and…

IntroI wish to share fundamental data about EntityFramework (EF) which needs to be sufficient to implement most of trivial duties working with relational database. This articles ought to assist younger builders to know EF higher and keep away from some errors which I did on my approach.PreparationFirstly lets put together a database. It displays most trivial case with submit and creator I made a decision to not invent a wheel as a result of this text reveals fundamental utilization of EF and never about some concrete mannequin.In mission we have to embrace correct nuget packagesdotnet add bundle Microsoft.EntityFrameworkCore -v 6.0.0besides EF bundle particular nuget for correct database dialect needs to be included. I used Npgsql bundle and Postgres as a database for examples on this article.dotnet add bundle Npgsql.EntityFrameworkCore.PostgreSQL -v 6.0.0EF preliminary configurationWe prepared use EF in our mission and it`s time to configure POCO courses which can signify database objects in .internet world.As you may see courses signify nearly the identical construction as a tables in database a distinction comes with digital properties in case of Creator it`s a listing of Posts and accordingly Publish entity has virutal Creator property. Such properties additionally known as as Navigation properties enable us to work with flat tables in objects world.We have to present correct configuration for EF with a purpose to make it understands navigation properties. Additionally Publish desk has serial sort in database and we have to inform EF about this moreover all properties needs to be correctly mapped to columns.I wish to share a data that EF offers us a approach to supply mapping between objects and tables in few alternative ways. I wish to go away entity objects easy as they’re proper now and never overwhelm them with tons of attributes. Fluent API offered by EF offers such capability. Making an attempt to make it extra clear and extra seen for future developer it`s higher to separate db mappings in numerous scoped information. One other good level is to make courses for mapping inner and forestall entry to them from one other tasks in resolution.In such mapping we outlined what property is major key for desk and mapped properties accordingly. Ultimately we outlined a navigation property and helped EF to know how entities associated to one another.word: we`ll use this mapping file a bit laterWe mapping for Posts seems to be much like authors. You may discover a sequence identify and a few code associated it. It comes from postgresql and in case you use one other database server you must present one other configuration for autoincremented column.And eventually we are able to create our personal database contextSmall Verify listWhat you need to keep away from throughout EF configurationselecting identify to your .internet entities adhere db desk identify and don`t use synonyms for thisprevious rule can also be relevant for navigation propertiestry to separate db mapping from one another it helps learn/evaluate code laterWe have to confirm that’s works and finest option to do it’s to begin utilizing it.word: All IO sure operation needs to be carried out in asynchronous approach, EF gives asynchronous analog to most of supported API.Add entities to databaseUnder the hood of AddAsync EF does one thing much like final strategy. It prepares set for correct entity sort and fasten entity with standing Added. This modifications tracked by ChangeTracker and once you name SaveChangesAsync it sends all knowledge to database.word: in case you are working with net servers, attempt to save modifications ultimately of http request it helps to avoid wasting knowledge integrity and forestall extra spherical journey to databaseGet entities from databaseAs you may see when key you may extract entity from database in a couple of methods. Generally FirstOrDefaultAsync helps however remember about monitoring settings. Should you don`t mark request to database AsNoTracking or don`t setup it on context stage, EF places your entity to ChangeTracker. It requires further sources from system for learn circulate it`s pointless.word: At all times use AsNotracking for learn operations in case you are not going to switch extracted entities laterLoad knowledge with navigation propertiesBy default once you load entity EF doesn`t load navigation properties to reminiscence. It signifies that once you entry navigation property risk to get NullReferenceException is excessive. It fairly handy to load entity along with navigation properties simply as soon as and use it for im reminiscence enterprise logic operations.word: Concentrate on lazy loading it may well trigger efficiency issues which laborious to identifyAs you see Embody permits us specify which navigation properties needs to be loaded from database.Use navigation properties to insert dataEF is sort of intelligent to know relation between objects and supply correct values for international keys.As you may see we are able to omit AuthorId and EF gives it for us.One other helpful technic which you’ll discover right here is to wrap modify circulate into transaction scope. In case of an issue transaction needs to be rollbacked and knowledge in database stays not corrupted.word: Working with net servers attempt to wrap calls which ought to modify knowledge into transaction and forestall any corrupted knowledge to be inserted into databaseConclusionsEF as ORM simplifies life and works with database easily. On this article yow will discover base examples the best way to configure and work with EF. Some examples comprise word which lets you enhance you each day interactions with EF. I’ll attempt to put together all finest practices the best way to work utilizing EF in some subsequent article.

Article Tags:
·
Article Categories:
Basic · knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *