EF5 Code First (error cannot be taken out of use)

Well, I pull my hair out because I just can't do many different relationships. I have the following two models:

public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } public string CompanyName { get; set; } public string FirstName { get; set; } public string Lastname { get; set; } public string EmailAddress { get; set; } public string PhoneNumber { get; set; } public bool? ChangePassword { get; set; } public bool? Deletable { get; set; } //Add more Properties for more fields public virtual IQueryable<CompanyInformation> ParentCompany { get; set; } } 

and

 public class CompanyInformation { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int id { get; set; } [DisplayName("Company Name:")] public string companyName { get; set; } [DisplayName("Website Address:")] [Url(ErrorMessage="The Website field is not a valid fully-qualified http, https, or ftp URL. (Example: http://www.website.com)")] public string website { get; set; } public string contactTitle { get; set; } [DisplayName("Contact First Name:")] public string contactFirstName { get; set; } //[Required] [DisplayName("Contact Last Name:")] public string contactLastName { get; set; } [Phone] [DisplayName("Phone Number:")] public string contactPhoneNumber { get; set; } [DisplayName("Address Display?")] public bool displayAddress { get; set; } [DisplayName("Phone Number?")] public bool displayPhoneNumber { get; set; } [DisplayName("Address 1:")] public string address1 { get; set; } [DisplayName("Address 2:")] public string address2 { get; set; } [DisplayName("City:")] public string city { get; set; } [DisplayName("State:")] public string state { get; set; } [DisplayName("Zip/Postal Code:")] public string zipCode { get; set; } [DisplayName("Search Engine?")] public bool allowSearchEngines { get; set; } //Navigation Property public virtual IQueryable<UserProfile> CompanyUsers{ get; set; } } 

I'm trying to make a many-to-many relationship between the two, and I just can't figure out how to do it right. I must mention that I am very new to EF Code First. My context class is as follows:

 public class myDB : DbContext { public SchedulerDB() : base("DefaultConnection") { } public DbSet<UserProfile> UserProfiles { get; set; } public DbSet<CompanyInformation> Companies { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<UserProfile>().HasMany(e => e.ParentCompanies).WithMany(e => e.CompanyUsers); } } 

ok As soon as I add modelBuilder above, I get the following error:

 The type arguments for method 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Scheduler.Model.UserProfile>.HasMany<TTargetEntity>(System.Linq.Expressions.Expression<System.Func<Scheduler.Model.UserProfile,System.Collections.Generic.ICollection<TTargetEntity>>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. C:\Users\Hiva\Documents\Project\ToDo\Infrastructure\myDB.cs 

What am I doing wrong? I can't seem to find examples that use modelBuilder differently to achieve the relationship between the two tables. Thank you for your help.

0
source share
1 answer

You should use ICollection for navigation properties:

 ICollection<UserProfile> CompanyUsers{ get; set; } 

and

 ICollection<UserProfile> ParentCompanies{ get; set; } 

instead of IQueriable

+2
source

Source: https://habr.com/ru/post/1207432/


All Articles