Entityframework Core, combine to database fields into model value.

Enterprise Complex 1 Reputation point
2021-05-02T22:16:22.783+00:00

Using entity framework core, doing custom model building code first, I want to store a system.numerics Vector2 in SQL server. In the database I represent the value as two distinct fields, 'ValueX' & 'ValueY'. How can I configure the entity in EF such that it will read the two fields from the database and convert them into the Vector2 using the values. Same question for writing the data...?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
743 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,211 Reputation points
    2021-05-02T23:54:23.76+00:00

    @Enterprise Complex

    My best answer is that you are going to have to save the two floats in their on respective SQL DB table columns.

    And you are going to have to have some kind of functional logic to combine them into Vector2.

    Maybe you can make the EF model class be a partial class with the second partial class file having functions that are solely working with Vector2. EF would never see the functions as it does EF CRUD operations, but you would be able to call the functions to do Vector2 stuff.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.