EF5代码优先(错误不能从使用推断) [英] EF5 Code First (Error cannot be inferred from the usage)
问题描述
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId {get;组; }
public string UserName {get;组; }
public string CompanyName {get;组; }
public string FirstName {get;组; }
public string Lastname {get;组; }
public string EmailAddress {get;组; }
public string PhoneNumber {get;组; }
public bool? ChangePassword {get;组; }
public bool?可删除{get;组; }
//为更多字段添加更多属性
public virtual IQueryable< CompanyInformation>家长公司{get;组;
}
和
public class CompanyInformation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id {get;组; }
[DisplayName(Company Name:)]
public string companyName {get;组; }
[DisplayName(Website Address:)]
[Url(ErrorMessage =网站字段不是有效的完全限定的http,https或ftp URL) http://www.website.com))]
public string website {get;组; }
public string contactTitle {get;组; }
[DisplayName(Contact First Name:)]
public string contactFirstName {get;组; }
// [必需]
[DisplayName(Contact Last Name:)]
public string contactLastName {get;组; }
[电话]
[DisplayName(Phone Number:)]
public string contactPhoneNumber {get;组; }
[DisplayName(Address Display?)]
public bool displayAddress {get;组; }
[DisplayName(Phone Number?)]
public bool displayPhoneNumber {get;组; }
[DisplayName(Address 1:)]
public string address1 {get;组; }
[DisplayName(Address 2:)]
public string address2 {get;组; }
[DisplayName(City:)]
public string city {get;组; }
[DisplayName(State:)]
public string state {get;组; }
[DisplayName(Zip / Postal Code:)]
public string zipCode {get;组; }
[DisplayName(Search Engine?)]
public bool allowSearchEngines {get;组; }
//导航属性
public virtual IQueryable< UserProfile>公司用户{get;组; }
}
我试图做一个很多这两者之间的许多关系和我无法弄清楚如何正确地做到这一点。我应该提到,我对EF Code First很新。
我的上下文类看起来像下面这样:
public class myDB:DbContext
{
public SchedulerDB()
:base(DefaultConnection)
{
}
public DbSet< UserProfile> UserProfiles {get;组; }
public DbSet< CompanyInformation>公司{get;组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< UserProfile>()。HasMany(e => e.ParentCompanies).WithMany(e = > e.CompanyUsers);
}
}
好的,一旦我添加上面的modelBuilder得到以下错误:
方法的类型参数System.Data.Entity.ModelConfiguration.EntityTypeConfiguration& .Model.UserProfile> .HasMany< TTargetEntity>(System.Linq.Expressions.Expression< System.Func< Scheduler.Model.UserProfile,System.Collections.Generic.ICollection< TTargetEntity>>>)'不能从用法。尝试显式指定类型参数。 C:\Users\Hiva\Documents\Project\ToDo\Infrastructure\myDB.cs
$ b $我做错了什么?我似乎没有找到任何使用modelBuilder的例子来实现两个表之间的多对多关系。感谢您的高级帮助。
您应该使用ICollection导航属性:
ICollection< UserProfile>公司用户{get;组; }
和
code>的ICollection<用户配置>家长公司{get;组;
而不是 IQueriable
Ok, I have been pulling out my hair because I simply cannot make a many to many relationship. 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 these two and I just can't figure out how to do it properly. I should mention that I am very new to the EF Code First. My Context Class looks like the following:
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 the 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 any examples that use the modelBuilder differently to achieve a many-to-many relationship between two tables. Thank you in advanced for your help.
You should use ICollection for navigation properties:
ICollection<UserProfile> CompanyUsers{ get; set; }
and
ICollection<UserProfile> ParentCompanies{ get; set; }
instead of IQueriable
这篇关于EF5代码优先(错误不能从使用推断)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!