Skip to content

Enable nullable ref type

moh-hassan edited this page Aug 14, 2022 · 1 revision

Starting OData2Poco v4.3.1, Nullable reference types of c# 8 can be generated with a new option -B (upper case) / --enable-nullable-reference.

Example

o2pgen -r https://services.odata.org/TripPinRESTierService -B -v

OR 

o2pgen -r https://services.odata.org/TripPinRESTierService --enable-nullable-reference -v

you see the properties type is attached with ?, for example:

public string? IataCode {get;set;}
public AirportLocation? Location {get;set;}
The Complete ouput code, click to expand!
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Spatial;

namespace Trippin
{
        public partial class Person
        {
            public string UserName {get;set;} //PrimaryKey not null

            public string FirstName {get;set;} // not null

            public string? LastName {get;set;}

            public string? MiddleName {get;set;}

            public PersonGender Gender {get;set;} // not null

            public long? Age {get;set;}

            public List<string>? Emails {get;set;}

            public List<Location>? AddressInfo {get;set;}

            public Location? HomeAddress {get;set;}

            public Feature FavoriteFeature {get;set;} // not null

            public List<Feature>? Features {get;set;}

        }

        public partial class Airline
        {
            public string AirlineCode {get;set;} //PrimaryKey not null

            public string? Name {get;set;}

        }

        public partial class Airport
        {
            public string? Name {get;set;}

            public string IcaoCode {get;set;} //PrimaryKey not null

            public string? IataCode {get;set;}

            public AirportLocation? Location {get;set;}

        }

        public partial class Location
        {
            public string? Address {get;set;}

            public City? City {get;set;}

        }

        public partial class City
        {
            public string? Name {get;set;}

            public string? CountryRegion {get;set;}

            public string? Region {get;set;}

        }

        public partial class AirportLocation : Location
        {
            public GeographyPoint? Loc {get;set;}

        }

        public partial class EventLocation : Location
        {
            public string? BuildingInfo {get;set;}

        }

        public partial class Trip
        {
            public int TripId {get;set;} //PrimaryKey not null

            public Guid ShareId {get;set;} // not null

            public string? Name {get;set;}

            public float Budget {get;set;} // not null

            public string? Description {get;set;}

            public List<string>? Tags {get;set;}

            public DateTimeOffset StartsAt {get;set;} // not null

            public DateTimeOffset EndsAt {get;set;} // not null

        }

        public partial class PlanItem
        {
            public int PlanItemId {get;set;} //PrimaryKey not null

            public string? ConfirmationCode {get;set;}

            public DateTimeOffset StartsAt {get;set;} // not null

            public DateTimeOffset EndsAt {get;set;} // not null

            public TimeSpan Duration {get;set;} // not null

        }

        public partial class Event : PlanItem
        {
            public EventLocation? OccursAt {get;set;}

            public string? Description {get;set;}

        }

        public partial class PublicTransportation : PlanItem
        {
            public string? SeatNumber {get;set;}

        }

        public partial class Flight : PublicTransportation
        {
            public string? FlightNumber {get;set;}

        }

        public partial class Employee : Person
        {
            public long Cost {get;set;} // not null

        }

        public partial class Manager : Person
        {
            public long Budget {get;set;} // not null

            public Location? BossOffice {get;set;}

        }

        public enum PersonGender
         {
                Male=0,
                Female=1,
                Unknown=2
        }
        public enum Feature
         {
                Feature1=0,
                Feature2=1,
                Feature3=2,
                Feature4=3
        }
}

Notes:

Enable nullable by using -B option include the actions of option -b (lower case) which is used for primitive types.

Fot the developer, a demo case is available online.

Try it online

Clone this wiki locally