locked
EntityFrameworkCore - Class with many list of the same class RRS feed

  • Question

  • User1461220549 posted

    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

    Saturday, April 25, 2020 10:33 AM

All replies

  • User-854763662 posted

    Hi  PiercarloBI ,

    For multiple one-to-many relationships between two entities , try to make the following changes in your model and DbContext:

    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 int FromEmailMessageId { get; set; }
            public EmailMessage FromEmailMessage { get; set; }
    
            public int BccEmailMessageId { get; set; }
            public EmailMessage BccEmailMessage { get; set; }
    }

    DbContext

    protected override void OnModelCreating(ModelBuilder builder)
    {
                builder.Entity<EmailMessage>()
                    .HasMany(em => em.FromAddresses)
                    .WithOne(fa => fa.FromEmailMessage)
                    .HasForeignKey(fa=>fa.FromEmailMessageId)
                    .IsRequired()
                    .OnDelete(DeleteBehavior.Restrict);
    
    
                builder.Entity<EmailMessage>()
                    .HasMany(em => em.BccAddresses)
                    .WithOne(fa => fa.BccEmailMessage)
                    .HasForeignKey(fa => fa.BccEmailMessageId)
                    .IsRequired()
                    .OnDelete(DeleteBehavior.Restrict);
    
    }

    Reference: https://stackoverflow.com/questions/54196199/entity-framework-core-multiple-one-to-many-relationships-between-two-entities

    Best Regards,

    Sherry

    Monday, April 27, 2020 2:38 AM
  • User-217098811 posted

    Hi PiercarloBI

    Shadow attributes are most commonly used for foreign key attributes. The relationship between two entities is represented by the foreign key value in the database. If a relationship is found, but the foreign key attribute cannot be found in the dependent entity class, the shadow attribute can be created according to the Convention.
    I think you can try setting foreign key properties in dependent entity classes.

    Best Regards

    Yinqiu

    Thursday, May 28, 2020 2:58 AM