I have two models named "User" and "IPSet". The user model is something like this:
public class Users
{
[Key]
public int UserCode { get; set; }
public string UserName { get; set; }
public ICollection<IPSet> IPSets { get; set; }
}
and IPSet model:
public class IPSet
{
[Key]
public int IPSetCode { get; set; }
public string IP { get; set; }
public Users Users { get; set; }
[ForeignKey("Users")]
public int UsersFK { get; set; }
}
The goal is setting Some IPs to a user in order to log in only from that IP addresses, So I need a One-To_Many relation between models. I've tried above codes and they're true.
But I want to have a column in IPSet named SetterUser that means which user has set an IP with a user.
SetterUser has also a relation with User table. How I can implement this relation?
I've tried:
public class Users
{
[Key]
public int UserCode { get; set; }
public string UserName { get; set; }
public ICollection<IPSet> IPSets { get; set; }
public ICollection<IPSet> IPSets1 { get; set; }
}
and
public class IPSet
{
[Key]
public int IPSetCode { get; set; }
public string IP { get; set; }
public Users Users { get; set; }
[ForeignKey("Users")]
public int UsersFK { get; set; }
public Users SetterUser{ get; set; }
[ForeignKey("SetterUser")]
public int SetterUserFK { get; set; }
}
But this error will appearance:
Unable to determine the relationship represented by navigation property 'IPSet.Users' of type 'Users'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Users
toUsers
, which in EF Core is represented with 2 one-to-many relationships. Note that if they were not self referencing, EF Core would automatically map them. So it's definitely duplicate. – Ivan Stoev Nov 5 '18 at 14:52