Entity Framework-빠른로드, 나열 할 모든 속성

크리스토스 바지 오티스

다음 모델이있는 간단한 프로젝트가 있습니다.

Customer 1--->N Addresses N<----1 City N<----1 Country
                     ^
                    N|
                     |
AddressTypes 1-------+

내 코드는 다음과 같습니다.

public partial class Customer {
        public Customer() {
            this.Address = new HashSet<Address>();
        }
        public int Id {get;set;}
        public string FirstName {get;set;}
        public string LastName {get;set;}
        public System.DateTime Birthday {get;set;}

        public virtual ICollection<Address> Address {get;set;}
    }

public partial class Address {
        public int Id {get;set;}
        public string Street {get;set;}
        public string Area {get;set;}
        public string Post {get;set;}
        public bool isDeleted {get;set;}

        public virtual Customer Customer {get;set;}
        public virtual City City {get;set;}
        public virtual AddressType AddressType {get;set;}
    }

public partial class AddressType {
        public AddressType() {
            this.Address = new HashSet<Address>();
        }
        public int Id {get;set;}
        public string Name {get;set;}

        public virtual ICollection<Address> Address {get;set;}
    }

public partial class Country {
        public Country() {
            this.City = new HashSet<City>();
        }
        public int Id {get;set;}
        public string Name {get;set;}

        public virtual ICollection<City> City {get;set;}
    }

public partial class City {
        public City() {
            this.Address = new HashSet<Address>();
        }
        public int Id {get;set;}
        public string Name {get;set;}

        public virtual ICollection<Address> Address {get;set;}
        public virtual Country Country {get;set;}
    }


public partial class MyModelContext : DbContext {
        public MyModelContext()
            : base("name=MyModelContext") {
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ProxyCreationEnabled = false;
        }

        public virtual DbSet<Country> Countries {get;set;}
        public virtual DbSet<City> Cities {get;set;}
        public virtual DbSet<AddressType> AddressTypes {get;set;}
        public virtual DbSet<Address> Addresses {get;set;}
        public virtual DbSet<Customer> Customers {get;set;}
    }

고객 목록을 반환하고 싶습니다. 처음에 나는 간단한 것을 시도 Customers.ToList()했지만 나는 exception during serialization이것이 순환 참조로 인한 것이라고 읽었습니다. 그래서 추가했습니다

Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = false;

그런 다음 고객을 확보 할 수 있었지만 주소는 없었습니다. 나는 시도했다 :

public List<Customer> getCustomers() {
    MyModelContext db = new MyModelContext();
    return db.Customers.Include("Address").ToList();
}

하지만 모든 속성을 검색하는 방법을 모르겠습니다. 각 주소의 도시, 국가, 주소 유형을 포함하여 모든 고객과 주소를 목록에 포함하고 싶습니다. 어떻게하니?

크리스토스 바지 오티스

이 방법으로 해결했습니다.

public List<Customer> getCustomers() {

            var customers = db.CustomerSet.Include("Address")
                .Include("Address.AddressType")
                .Include("Address.City")
                .Include("Address.City.Country")
                .ToList();

            return customers;
        }

나는 고객의 모든 속성을 명시 적으로 작성해야하고 내가 원하는 일을하는 일반적인 방법은 없지만 작동하는 것을 정말 좋아하지 않습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Entity Framework Core 2.0.1 모든 중첩 관련 엔터티에 대한 빠른로드

Entity Framework 6 코드 우선 모든 열을 생성하지 않음

Entity Framework를 사용하여 큰 문자열 속성이있는 엔터티를로드 할 때 성능 저하

속성을 탐색 속성으로 구성 할 수 없습니다.-Entity Framework

나열 할 Entity Framework 제네릭 DbSet

Entity Framework Core 2.2의 빠른로드

모든 열이 포함 된 Entity Framework

Entity Framework Core 프로젝트를 빌드 할 때 종속성 오류

SQL Server CE의 Entity Framework-지연 및 빠른로드, 성능 고려 사항

Entity Framework 5 빠른로드. 쿼리는 어떻게 작성됩니까?

Entity Framework 및 Web API / REST-속성 나열에 조인

Entity Framework Core-소수 정밀도 및 배율을 모든 소수 속성으로 설정

Entity Framework를 사용하여 하나의 열을 업데이트하고 모든 값을 1로 설정

먼저 Entity Framework 코드, 부울 속성에 열 이름 설정

Entity Framework Core의 다른 컨텍스트에서 속성로드

Javascript 객체의 모든 프로토 타입 속성 나열

Entity Framework Core : 메모리 내 데이터 저장소를 사용할 때 탐색 속성로드를 테스트하는 방법

Entity Framework 탐색 속성에 프로젝션을 사용할 수 있습니까?

Entity Framework Core LINQ 클래스 속성 / 메소드는 포함 된 속성을 사용할 수 없습니다.

먼저 Entity Framework 코드의 모든 문자열에 대해 MaxLength 설정

Entity Framework Core : F #에서 Option <T> 유형의 모든 속성에 변환 적용

모든 확장 속성을 어떻게 표시 / 나열 할 수 있습니까?

Entity Framework : 개체로 속성 선택

Entity Framework Core의 고유 한 열을 기반으로 모든 행 선택

빠른로드시 Entity Framework 6로드 목록

Entity Framework 종속성을로드하지 않고 Dapper와 함께 사용할 Nuget SQLite

Entity Framework-탐색 속성을로드 할 필요없이 여러 테이블에 여러 행 추가

Entity Framework 확장 대량 메서드를 사용할 때 마지막으로 수정 된 속성

클래스의 모든 속성 나열