【问题标题】:EF5 Code First (Error cannot be inferred from the usage)EF5 Code First(无法从用法中推断出错误)
【发布时间】:2013-01-16 05:03:11
【问题描述】:

好的,我一直在拔头发,因为我根本无法建立多对多的关系。我有以下两种型号:

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 非常陌生。 我的上下文类如下所示:

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

}

好的,只要我添加上面的模型构建器,我就会收到以下错误:

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

我做错了什么?我似乎找不到任何以不同方式使用模型构建器来实现两个表之间的多对多关系的示例。非常感谢您的帮助。

【问题讨论】:

    标签: c# linq entity-framework ef-code-first entity-framework-5


    【解决方案1】:

    您应该将 ICollection 用于导航属性:

    ICollection<UserProfile> CompanyUsers{ get; set; }
    

    ICollection<UserProfile> ParentCompanies{ get; set; }
    

    而不是IQueriable

    【讨论】:

    • 有什么区别?就像我说的我很新,并认为通过使用 IQueriable,我可以传递 linq 查询,系统将在 sql server 上执行它并只返回子集。
    • ICollection 是 UserProfile 类型的元素的集合,但 IQueriable 可以是任何东西。您可以将 ICollection 和 IList 用于导航属性。在我看来,您的导航属性集合应该有 Add 方法。
    • 添加方法?对不起,就像我说的那样,我很新,我正在学习。我的导航属性应该有一个 add 方法是什么意思?
    • 您的导航属性是实体的集合(例如 Userprofile)。 EF(内部)从数据读取器中获取这些实体,并且应该有可能将其添加到集合中,因此您的集合应该使用 Add 方法(IList 或 ICollection)实现接口。
    • 哦,好的,谢谢您的澄清。现在一切都完美无缺(到目前为止很好:))
    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2012-08-14
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多