0

좋아, 나는 단순히 많은 관계를 만들 수 없기 때문에 나는 내 머리카락을 꺼냈다.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에 대해 아주 새로운 것을 언급해야한다. 내 상황에 맞는 클래스처럼 보이는 다음

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 

내가 잘못하고있는 중이 야 무엇을? 두 테이블 간의 다 대다 관계를 달성하기 위해 modelBuilder를 다르게 사용하는 예제를 찾을 수 없습니다. 도와 주셔서 진심으로 감사드립니다.

답변

2

당신은 탐색 속성은 ICollection을 사용해야합니다

ICollection<UserProfile> CompanyUsers{ get; set; } 

대신

+0

차이점은 무엇입니까 IQueriable

ICollection<UserProfile> ParentCompanies{ get; set; } 

를? 내가 아주 새롭다 고 말했듯이 IQueriable을 사용하면 linq 쿼리를 전달할 수 있으며 시스템은 SQL Server에서이 쿼리를 실행하고 하위 집합 만 반환합니다. – hjavaher

+0

ICollection 은 UserProfile 유형의 요소 모음이지만 Iwweriable 은 무엇이든 지정할 수 있습니다. 탐색 속성에 ICollection 및 IList를 사용할 수 있습니다. 내게 네비게이션 프로퍼티 컬렉션에는 Add 메소드가 있어야하는 것 같다. –

+0

메서드 추가? 미안해, 내가 말했듯이, 나는 아주 새롭고 내가 따라갈 때 배우고있다. 내 탐색 속성에 add 메서드가 있어야한다는 것은 무엇을 의미합니까? – hjavaher

관련 문제