IT:AD:EF/CodeFirst:HowTo:Define Models/Relationships/Examples/n-0...1 (Many-To-One, Optional)

Summary

One can have Categories (1), with yet having a single entity using them (*) :

invoiceCategoryreferences*1

Process

        //Many to One, Optional
        ////0..*
        modelBuilder.Entity<Invoice>()
            //Make it optional that Invoice has a Category applied before it is saved:
            .HasOptional(i => i.Category2)
            //
            .WithMany(); //1-n

Working with:


    public class Category
    {
        public virtual int Id { get; private set; }
        //Note that we can remove Convention expecting ICollection<Invoice> 
        //and it will still create Relationship, as it is defined in FluentAPI
        public virtual bool Display { get; set; }
        public virtual string Text { get; set; }
    }