Readonly Fields or Properties

Is there a way to have a read -only field or a Property without binding to a database column with the first Entity Framework Code?

I found that both are ignored, for example:

using System; namespace App.Model { public class Person { string _address1; // Private Field to back the Address Property public string Address1 // Public Property without a Setter { get { return _address1; } } public readonly string Address2; // Public Read Only Field } } 

Is there a Fluent API call or other approach?

+6
source share
1 answer

It is not possible to make objects truly immutable, but you can use an inaccessible setter so that users cannot modify them, which can be good enough, depending on your situation:

 public string Address1 // Public Property without a Setter { get { return _address1; } internal set { _address1 = value; } } 

The Entity Framework will still be able to set the value, so it will load correctly, but after creation, the property will be more or less fixed.

(I recommend using the internal installer rather than private , so you can still map to the Fluent API in your context or configuration class, provided that they are in the same assembly or friends assembly.)

+5
source

Source: https://habr.com/ru/post/945808/


All Articles