none
Nullability when reusing a Complex Type for two columns in a table RRS feed

  • Question

  • Hello,

    I have a Complex Type called Phone which has property named Number.

    In a table named Contact, there are two columns that I want to map to the Phone type. One column is PrimaryPhone and NOT nullable, the other one is SecondaryPhone and nullable.

    However, the configuration with fluent API complains that nullability conflicts.This also happens when the Phone type is used in other tables with different nullability.

    I know it can be solved if I create another type such as SecondaryPhone inheriting Phone, but I don't think it's feasible to create sub types everytime I need the type in different tables.

    Is there a way to achieve this?

    Thanks.



    Friday, May 24, 2013 11:16 PM

All replies

  • Hi Jake,

    Would you like to show some code how you implement nullable complex types with Fluent API?

    According to this blog: http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx

    If I defined the classes like:

    public class Phone
    {
        public string Number { get; set; }
    
        public bool IsNull()
        {
            return string.IsNullOrEmpty(Number);
        }
    }
    
    public class Contact
    {
        public Contact()
        {
            PrimaryPhone = new Phone();
            SecondaryPhone = new Phone();
        }
    
        public int ContactId { get; set; }
        public Phone PrimaryPhone { get; set; }
        public Phone SecondaryPhone { get; set; }
    }
    
    public class PhoneContext : DbContext
    {
        public DbSet<Contact> Contacts { get; set; }
    }
    

    The generated Contacts table in the database has three columns:

    ContactId (PK), PrimaryPhone_Number, SecondaryPhone_Number.

    The second and the third columns' names are generated by convension.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 27, 2013 8:59 AM
    Moderator
  • Hi Chester,

    I have a separate configuration class for Contact, and in the ContactConfiguration class:

    Property(x => x.PrimaryPhone.Number).HasColumnName("PrimaryPhone").IsRequired();

    Property(x => x.SecondaryPhone.Number).HasColumnName("SecondaryPhone").IsOptional();

    And let's say there's another entity that uses the Phone complex type, such as Company. I would configure it something like:

    Property(x => x.PhoneNumber.Number).HasColumnName("PhoneNumber").IsRequired();

    Property(x => x.FaxNumber.Number).HasColumnaName("FaxNumber").IsOptional();

    The Company entity would be defined something like:

    public class Company

    {

       public Phone PhoneNumber { get; set; }

       public Phone FaxNumber { get; set; }

    ....

    }

    Either mapping the Phone in the same table with different column name or mapping the Phone in two different tables, mapping it to different column names with HasColumnName works fine, but mapping to different nullability does not work.

    Regards,

    Jake

    Monday, May 27, 2013 5:39 PM