Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using external object in entity framework object

I'm trying to use Entity Framework to store & retrieve some objects, one of the properties of which is a class which I have no control over.

For the sake of this problem description let's say I'm trying to persist Arc objects:

class Arc {
   public Angle start { get; set; }
   public Angle end { get; set; }
   // ... other properties ...
}

but I don't have write access to the Angle source, which has a public interface something like this:

class Angle {
   public enum Unit { Radians, Degrees };
   public Angle(double value, Unit units); // constructor
   public float Radians() { get; } // returns angle value in radians
   public float Degrees() { get; } // returns angle value in degrees
}

I presume I can do something in the DbContext's OnModelCreating() method to map the angle fields to a float column in the database but despite reading the MSDN docs, I've not been able to figure out what I need to do to accomplish this.

I somehow need to read the angle's Degrees (or Radians) property when storing the objects, then construct a new Angle using that value when retrieving them. How can I do this?

EDIT: I've thought of a poor solution, which is to make the Angle properties not mapped, and add an additional mapped property for each to do the conversion, something like this:

public float start_degrees { get { return start.Degrees; }
                              set { start = new Angle(value, Angle.Degrees); }
                             }

but I'd rather avoid this, because my object has many properties of this nature, so it would mean adding lots more. I'm hoping there's a much simpler solution.

like image 831
Dave W Avatar asked Nov 30 '25 12:11

Dave W


1 Answers

Probably I missunderstood the question, but let me give it try. How about this?

public class MyOwnTypeOfAngle
{

    public MyOwnTypeOfAngle(Angle input)
    {
        this.Radians = input.Radians;
        this.Degrees = input.Degrees;
    }


    public double Radians { get; set; }
    public double Degrees { get; set; }
}

In any case, when you want to change the configuration of your DbSets in EF, you set the following:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MyNewConfigurationType ());
    }

UPDATE ok, I haven't gotten so deep into configurations but I hope this may lead you in the right direction:

class MyNewConfigurationType : EntityTypeConfiguration<Arc>
 {
    public MyNewConfigurationType()
    {

        Ignore<Angle>(y => y.start);
        Ignore<Angle>(y => y.end);

        Property<float>(x => x.start.Degrees).HasColumnName("myStartDegrees");
        Property<float>(x => x.end.Degrees).HasColumnName("MyEndDegrees");
    }
}
like image 141
Jorge Alvarado Avatar answered Dec 03 '25 00:12

Jorge Alvarado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!