Consider an ORM that maps tables to classes, and columns to properties. How should it handle a property that maps to an identity column for non-inserted objects?For example,[Table(MyTable)]public class MyMappedType{    [Column(Id)]    public int Id     {         get        {             // ...        }    }}MyMappedType myObj = new MyMappedType();int id = myObj.Id; // Object not inserted into database so value is missingThere are a few different options,Have the property getter throw an exception. This seems the most proper because if Id has not been assigned, any value returned is meaningless. However, it goes against guidelines stating property get methods should not throw exceptions.Have the property return a special value, i.e. 0 or -1. I believe in most ORM's the key properties are initialized with the default (0). However, this could be misleading if there is a row in the table with identity column value of 0.Force Id properties to be type Nullable<int> so it can return null until the value gets assigned.Which option is best?
How should an ORM handle unassigned Id property?
c#;orm
You answered yourself already. There is a reason most other ORMs go with option #2.#1 should not even be considered, as throwing an exception from a property is extremely counterintuitive for the final user.#3 is better, and mostly mirrors #2, in the sense that it assigns a default value (null in this case) to an unassigned property. However, Nullable<T> types are tricky to use, and would force your users to always check for HasValue before comparing, and other annoying complexity.In the end, the only downside to #2 is that you can't have a row with an id of default(T), but that doesn't even makes any sense most of the time.Also note that you aren't forced to have int as your primary key. OWIN default ApplicationUser implementation, for example, uses Guid as its primary key type (uniqueidentifier in SQL Server).  