Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The foreign key property 'fk1' was created in shadow state because a conflicting property with the simple name 'property'

I'm creating my database using code-first and entity framework core, I have a 1:1 relationship, but in this relationship, I have a navigation property in both entities, this is my first entity:

public class User
    {
        [Key]
        public string Email { get; set; }
        public virtual Mac Mac { get; set; }
        public virtual Gender Gender { get; set; }        
    }

and this is my second entity:

public class Mac
    {
        [Key]
        public Guid Id { get; set; }
        public DateTime CreationDate { get; set; }
        public HashSet<MacsUsers> MacsUsers { get; set; }
        public string UserEmail { get; set; }
        public User User { get; set; }
    }

and this is my dbContext configuration:

builder.Entity<User>().HasOne(u => u.Mac)
                .WithOne()
                .HasForeignKey<Mac>(g => g.UserEmail);

THE REASON why I have these navigation properties in both entities is that I have a query in which I retrieve data from Mac by using User entity like this:

foreach (var item in ListTupla)
                {
                    item.CreationDate = listUser.FirstOrDefault(u => u.Mac.Id == item.MacId).Mac.User.CreationDate;
                    item.MacUserTalked = listUser.FirstOrDefault(u => u.Mac.Id == item.MacId).Mac.User.Email;//here Mac.User
                    item.MacUserWhoTalks = listUser.FirstOrDefault(u => u.Mac.Id == item.MacId).Email;
                    item.Name = listUser.FirstOrDefault(u => u.Mac.Id == item.MacId).Mac.User.Name; //here Mac.User

this is exactly the message warning: The foreign key property 'Mac.UserEmail1' was created in shadow state because a conflicting property with the simple name 'UserEmail' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core

I have found a solution is putting the virtual in the navigation property but it does not work for me, any comment?

like image 942
Andres Guillen Hernandez Avatar asked Dec 21 '25 17:12

Andres Guillen Hernandez


1 Answers

I added public virtual in front of my reference navigation properties and it solved the issue.See this image please

like image 165
Muhammad Salman Avatar answered Dec 23 '25 08:12

Muhammad Salman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!