Skip to content

Entity Framework can't understand foreign key and trying add “_Id” column to query #377

@pavelkatkovsky

Description

@pavelkatkovsky

Hello,
I am trying to map two table through one.
Here is my db structure:
image

Here is my models:

public class Notification
    {
        public Notification()
        {
            Accounts = new HashSet<Account>();
        }
        public Guid Id { get; set; }
        public NotificationType NotificationType { get; set; }
        public virtual ICollection<Account> Accounts { get; set; }
        public virtual NotificationModelRequest NotificationModelRequest { get; set; }
    }

    public class NotificationModelRequest
    {
        public Guid NotificationId { get; set; }
        public Guid ModelId { get; set; }
        public Guid AccountId { get; set; }
        public virtual Notification Notification { get; set; }
        public virtual Model Model { get; set; }
        public virtual Account Account { get; set; }
    }
    public partial class Model
    {
        public Guid id { get; set; }
        public virtual NotificationModelRequest NotificationModelRequest { get; set; }
    }

    public partial class Account
    {
         public Account()
        {
            Notifications = new HashSet<Notification>();
        }
        public Guid id { get; set; }
        public virtual NotificationModelRequest NotificationModelRequest { get; set; }
    }

Here is my mappings:

        public NotificationConfiguration()
        {
            ToTable("Notification");
            HasKey(x => x.Id);
            Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            Property(x => x.NotificationType);

            HasMany(e => e.Accounts)
                .WithMany(c => c.Notifications)
                .Map(m =>
                {
                    m.MapLeftKey("NotificationId");
                    m.MapRightKey("AccountId");
                    m.ToTable("AccountNotifications");
                });

            Ignore(x => x.ModelName);
            Ignore(x => x.CreatedBy);

            HasRequired(x => x.NotificationModelRequest).WithRequiredPrincipal(x => x.Notification);
        }

        public NotificationModelRequestConfiguration()
        {
            ToTable("NotificationModelRequest");
            HasKey(x => x.NotificationId);

            Property(t => t.ModelId).HasColumnName("ModelId");
            Property(t => t.AccountId).HasColumnName("AccountId");

            HasRequired(x => x.Model).WithMany().HasForeignKey(x => x.ModelId);
            HasRequired(x => x.Account).WithMany().HasForeignKey(x => x.AccountId);
        }

        public AccountConfiguration()
        {
            ToTable("Account");
            HasKey(x => x.id);
        }

        public ModelConfiguration()
        {
            ToTable("Model");
            HasKey(x => x.id);
        }

When i am trying make include the nested properties i have error:

        public async Task<List<Notification>> GetNotificationByAccountId(Guid accountId)
        {
            var query = GetQuery()
                .Include(x => x.NotificationModelRequest)
                .Include(x => x.NotificationModelRequest.Model)
                .Include(x => x.NotificationModelRequest.Account)
                .Include(x => x.Accounts);

            var testQuery = _context.Set<NotificationModelRequest>()
                .Include(x => x.Model)
                .Include(x => x.Account);
            var test = testQuery.FirstOrDefault();

            return await query.ToListAsync();
        }

Msg 207, Level 16, State 1, Line 15
Invalid column name 'NotificationModelRequest_NotificationId'.
Msg 207, Level 16, State 1, Line 37
Invalid column name 'NotificationModelRequest_NotificationId'.

Could you please help how to i have to write this mapping?
Thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions