Home > OS >  C# EF Core converting object to string / get; set
C# EF Core converting object to string / get; set

Time:01-04

I do get a stack overflow when trying to convert an object to a string in C#. I´m making an API Call to an endpoint and pass the response in this class. As the ef core does not support the datatype "object" i´m trying to convert this datatype to string to store it in the database as a column.

I have a class with various attributes like

        public DateTime? start_date_local { get; set; }
        public string? timezone { get; set; }
        public double? utc_offset { get; set; }

some of them are from type object:

        [NotMapped]
        public object? start_latlng { get; set; }

as this datatype is not supported, I´m not mapping this to the DB but I´m trying to convert this into a string and store it within a second datatype which can be inserted into the DB.

        public string start_latlng2
        {
            get { return start_latlng2; }
            set { start_latlng2 = Convert.ToString(start_latlng); }
        }

This does not seem to work as I always get an error like:

Stack overflow.
Repeat 19126 times:
--------------------------------
   at SportAnalytics.DataModel.Activity.get_start_latlng2()
--------------------------------
   at DynamicClass.lambda_method171(System.Runtime.CompilerServices.Closure, Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.EnsureOriginalValues()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntrySubscriber.SnapshotAndSubscribe(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry)

Am I doing something wrong? Is there a better way to achieve what I´m trying to do?

Best, Chris

CodePudding user response:

You can try like this

public class Test{      
   public Test1 start_latlng {get;set;}  
}

public class Test1{       
   public string Attr1 {get;set}
   public string Attr2 {get;set}
}

CodePudding user response:

I actually don´t want to use that object type, but the API response is for that attribute of type object, so this isn´t something I can change, or?

  • Related