Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

EntityFrameworkCore - Class with many list of the same class

$
0
0

Hi guys,

I have following class that have many List of the same classes :

public class EmailMessage
    {

        #region Lists

        
        public List<EmailAddress> FromAddresses { get; set; }
        public List<EmailAddress> BccAddresses { get; set; }
        #endregion

        [Key]
        public int Id { get; set; }
        public string MessageId { get; set; }
        public uint Uid { get; set; }
        public DateTimeOffset Date { get; set; }
        public MessageImportance Importance { get; set; }
        public MessagePriority Priority { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; } = true;
        public bool NeedReadReceipt { get; set; }
        public bool NeedDeliveredReceipt { get; set; }
        public string InReplyTo { get; set; }
        public string Sender { get; set; }
        public EmailMessageDirection  MessageDirection { get; set; }
        public User User { get; set; }

    }
 public class EmailAddress
    {
        private string _name;

        public EmailAddress(EmailAddress address)
        {
            _name = address.Name;
            Address = address.Address;
        }
        public EmailAddress(string name, string address)
        {
            _name = name;
            Address = address;
        }

        [Key]
        public int Id { get; set; }
        public string Name
        {
            get
            {
                if (string.IsNullOrEmpty(_name))
                {
                    return Address;
                }
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public string Address { get; set; }

        
        public EmailMessage FromEmailMessage { get; set; }
        
        public EmailMessage BccEmailMessage { get; set; }
        

    }

My data Context is following:

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<EmailMessage>()
                .HasMany(em => em.FromAddresses)
                .WithOne(fa => fa.FromEmailMessage)
                .IsRequired();


            builder.Entity<EmailMessage>()
                .HasMany(em => em.BccAddresses)
                .WithOne(fa => fa.BccEmailMessage)
                .IsRequired();

        }

When I do the migration it build only one Email Address Table with following Shadow properties:

FromEmailMessageId and BccEmailMessageId

with foreign key on EmailMessage Id.

This does not work because when I tra to add a message I get an error that On EmailMessage Table is missing FromEmailMessageId or BccEmilMessageId field

So the question is I can I force builder to make distinct tables for Each list in EmailMessage class , without shadow properties ?

Thankyou for help


Viewing all articles
Browse latest Browse all 9386

Trending Articles