locked
EF Code First with complex class structure RRS feed

  • Question

  • Hello:

    I have a complex class structure that I want to save to a database. It is complex to explain so I'm going to show you my model classes:

    public class Headend
    {
        public Configuration Cfg { get; set; }
        public List<Module> Modules { get; set; }
    }
    
    public class Configuration
    {
        public S1Data Data { get; set; }
        public S2Standard Standard { get; set; }
        public S3Alarms Alarms { get; set; }
    }
    
    public class S1Data
    {
        public string Time { get; set; }
        public string Date { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public List<string> TypeOptions { get; set; }
        public bool ChannelCheck { get; set; }
    }
    
    public class S2Standard
    {
        public bool StandardCheck { get; set; }
        public string Standard { get; set; }
        public List<string> StandardOptions { get; set; }
    }
    
    public class S3Alarms
    {
        public bool HwAlarm { get; set; }
        public bool PowerAlarm { get; set; }
        public bool TemperatureAlarm { get; set; }
        public float Temperature { get; set; }
        public float TemperatureMin { get; set; }
        public float TemperatureMax { get; set; }
        public float TemperatureInc { get; set; }
    }
    
    public class Module
    {
        public S0Configuration Cfg { get; set; }
        public S1Status Status { get; set; }
        public S2Services Services { get; set; }
    }
    
    public class S0Configuration
    {
        public byte PositionX { get; set; }
        public byte PositionY { get; set; }
        public ModuleTypes Type { get; set; }
        public string Name { get; set; }
        public string HwVersion { get; set; }
        public string Fw1Version { get; set; }
        public string Fw2Version { get; set; }
    }
    
    public class S1Status
    {
        public string StatusCode { get; set; }
        public string StatusString { get; set; }
    }
    
    public class S2Services
    {
        public List<Service> Services1 { get; set; }
        public string Bw1 { get; set; }
        public List<Service> Services2 { get; set; }
        public string Bw2 { get; set; }
    }
    
    public enum ModuleTypes
    {
        TypeA,
        TypeB,
        TypeC,
        TypeD
    }
    
    public struct ProgramStruct
    {
        public int Input;
        public string Name;
        public string Active;
        public List<string> ActiveOptions
        public string Bw;
        public string Number;
    }

    As you can see I have several levels of classes, I use lists, enumerations and structs. I have more classes (S3, S4, S5...) but I think that this example is enough. I'm not sure about the changes I have to do to use theses classes with Code First. Do I have to add identifiers to the lists? For example:

    public class S2Standard
    {
        public bool StandardCheck { get; set; }
        public string Standard { get; set; }
        public List<string> StandardOptions { get; set; }
    }

    Dot I need to change it to this?

    public class S2Standard
    {
        public bool StandardCheck { get; set; }
        public string Standard { get; set; }
        public virtual ICollection<StandardOption> StandardOptions { get; set; }
    }
    
    public class StandardOption
    {
        [Key]
        [Required]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
    }

    Do I have to add Id to all classes? Using structs or enumerations can cause problems? Is it anything else I need to know? In this case:

    public class Headend
    {
        public Configuration Cfg { get; set; }
        public List<Module> Modules { get; set; }
    }
    
    public class Configuration
    {
        public S1Data Data { get; set; }
        public S2Standard Standard { get; set; }
        public S3Alarms Alarms { get; set; }
    }

    I need something in properties to use Headend.Cfg.Standard.StandardCheck?

    It is my first time using Code First, and I'm not sure about the changes I need to do.

    Thank you,

    Jon.

    Tuesday, May 24, 2016 11:19 AM

Answers

  • Hi Jon 123,

    >>Do I have to add identifiers to the lists?

    From my experience, it seems that ef doesn't support List<string>, which couldn't map to database.

    >>Dot I need to change it to this?

    Yes, you need change it to this.

    >>Do I have to add Id to all classes?

    Entity Framework relies on every entity having a key value that it uses for tracking entities. One of the conventions that code first depends on is how it implies which property is the key in each of the code first classes. That convention is to look for a property named “Id” or one that combines the class name and “Id”, such as “BlogId”. The property will map to a primary key column in the database.

    You could use the key annotation to specify which property is to be used as the EntityKey.

    for more information, please refer to:

    https://msdn.microsoft.com/en-us/data/jj591583.aspx

    >>Using structs or enumerations can cause problems? Is it anything else I need to know?

    As far as i know, it seems that ef does't support structs, but support enum with ef5 and later versions. for more information, please refer to:

    https://msdn.microsoft.com/en-us/data/hh859576.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 25, 2016 2:55 PM