Read Only Properties

0

I am working with Entity Framework. I have a class Compra , when I put a read-only property, what function does it have? Does it come as a property in memory? Is it no longer in the map that is done with the fluent api?

I'm working with Code-First. I show the class below:

public class Compra
{
    public Compra()
    {
        this.DetalleCompras = new List<DetalleCompra>();
    }
    public int CompraId { get; set; }
    public string NumeroDocumento { get; set; }
    public int ProveedorId { get; set; }
    public int TipoComprobanteId { get; set; }
    public EnumTipoMoneda TipoMoneda { get; set; }
    public int SolicitudOrdenId { get; set; }
    public int CondicionPagoId { get; set; }
    public DateTime FechaEmision { get; set; }
    public string GuiaRemision { get; set; }
//        public decimal Total { get; set; }
    public decimal Total
    {
        get { return this.DetalleCompras.Sum(x => x.Precio * x.Cantidad); }
    }

    public virtual Proveedor Proveedor { get; set; }
    public virtual TipoComprobante TipoComprobante { get; set; }
    public virtual SolicitudOrden SolicitudOrden { get; set; }
    public virtual CondicionPago CondicionPago { get; set; }

    public virtual ICollection<DetalleCompra> DetalleCompras { get; set; }
    public virtual ICollection<PagoProveedor> PagoProveedores { get; set; }
}
    
asked by Pedro Ávila 22.08.2016 в 15:38
source

2 answers

3
  

a read-only property, what function does it have?

A property that has only get means that its value can not be varied (due to the absence of set ) from said property; However, nothing will prevent you from changing its value from another public property or from within the class itself. This is useful to follow the Encapsulation principles of Orientation oriented to objects

  

Does it come as a property in memory?

It does not have anything to do with the properties or methods that you use to access data of the class with the representation that the class has in memory. In general, the information that forms an object is stored in an adjoining manner (this may vary depending on the object and the language) and the access properties (both read-only and read-write) access the said memory package, the quantity or type of methods of an object do not change how the object is stored in memory.

  

Is there no longer a condidera in the map that is done with the fluent api?

I do not understand the question, anyway the best way to find out is to try it out.

    
answered by 22.08.2016 / 16:15
source
0

> > Are you no longer in the map with the fluent api?

From fluent you can indicate a property that does not map with a column of the table

Specifying Not to Map to CLR Property to a Column in the Database

basically it would be

modelBuilder.Entity<Compra>().Ignore(t => t.Total);

> > Does it come as a property in memory?

I do not believe that the correct concept is in memory since the entity itself is in memory, basically it is a calculated property that in this case works with the data of the other properties

    
answered by 22.08.2016 в 16:09