EF5代码优先(不能从用法中推断出错误)



好吧,我一直在绞尽脑汁,因为我根本无法建立多对多的关系。我有以下两种型号:

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; }
}

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; }
}

我试图在这两者之间建立一个多对多的关系,我只是不知道如何正确地做它。我应该提到的是,我对EF Code First是非常陌生的。我的Context类如下所示:

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);            
    }
}

好的,只要我添加上面的modelBuilder,我得到以下错误:

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:UsersHivaDocumentsProjectToDoInfrastructuremyDB.cs

我做错了什么?我似乎找不到任何使用modelBuilder以不同方式实现两个表之间多对多关系的示例。提前感谢您的帮助。

你应该使用ICollection作为导航属性:

ICollection<UserProfile> CompanyUsers{ get; set; }

ICollection<UserProfile> ParentCompanies{ get; set; }

代替IQueriable

相关内容

  • 没有找到相关文章

最新更新