I have an old database that I can't change now. In the database there are no relation amongst tables. Please see the below DB image. Here three tables which are defines many to many relation but no physically relation (foreign key). With EFCore DB first approach, Now I need to map EFCore models with navigator. I have created the model classes as below,
public class Company {
[Key]
[MaxLength(36)]
[Column("COMPANY_ID")]
public string CompanyId { get; set; }
[Column("FULL_NAME")]
public string FullName { get; set; }
/*Others properties*/
public virtual ICollection<TagCompany> TagCompanies { set; get; }
}
public class Tag {
[Column("TAG_GROUP_ID")]
public string GroupId { get; set; }
[Column("TAG_ITEM_ID")]
public string Id { get; set; }
[Column("TAG_ITEM_NAME")]
public string Name { get; set; }
[Column("TAG_ITEM_DESCRIPTION")]
public string Description { get; set; }
public virtual ICollection<TagCompany> TagCompanies { set; get; }
}
public class TagCompany {
[Column("COMPANY_ID")]
public string CompanyId { get; set; }
[Column("TAG_ITEM_ID")]
public string TagId { get; set; }
public virtual Company Company { set; get; }
public virtual Tag Tag { set; get; }
}
And the OnModelCreating methods as below,
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Company>().HasKey(o => o.CompanyId);
modelBuilder.Entity<Tag>().HasKey(o => new { o.GroupId, o.Id });
modelBuilder.Entity<TagCompany>().HasKey(o => new { o.CompanyId, o.TagId });
modelBuilder.Entity<TagCompany>().HasOne(s => s.Tag).WithMany(s => s.TagCompanies).HasPrincipalKey(o => new { o.GroupId, o.Id });
modelBuilder.Entity<TagCompany>().HasOne(s => s.Company).WithMany(s => s.TagCompanies).HasForeignKey(s => s.CompanyId);
}
With above code I am facing below error,
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'TagGroupId'. Invalid column name 'TagId1'. at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at
