0

EF6을 사용하여 MVC5 인터넷 응용 프로그램을 코딩 중이며 외래 키 이름과 관련하여 질문이 있습니다.Entity Framework 6 외래 키

public int mapLocationListGalleryId { get; set; } 
public virtual MapLocationListGallery mapLocationListGallery { get; set; } 

EF 테이블을 생성 할 때,이 모두 다음과 같은 열 :

나는이 두 필드가 MapLocationList라는 모델이

을 MapLocationListGallery_Id mapLocationListGalleryId

누군가 제발 explai 수 있습니다. n MapLocationListGallery 외래 키에 대해 두 개의 열이있는 이유는 무엇입니까? 사전

편집에

덕분에

내가 대문자 M을 사용하기 위해 이름을 변경, 아직 추가 열이 아직도있다. 나뿐만 아니라이 방법을 사용하고 난이 문제가 발생하지 않습니다

modelBuilder.Entity<MapLocationListGallery>() 
    .HasRequired(c => c.thumbnailAsset) 
    .WithMany() 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<MapLocationList>() 
    .HasRequired(c => c.thumbnailAsset) 
    .WithMany() 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<MapLocationList>() 
    .HasRequired(c => c.mapLocationListGallery) 
    .WithMany() 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<MapLocationListItem>() 
    .HasRequired(c => c.thumbnailAsset) 
    .WithMany() 
    .WillCascadeOnDelete(false); 

답변

1

:

public class MapLocationList : IMapLocationItemWithAssets 
{ 
    [Key] 
    public int Id { get; set; } 
    [Required] 
    public string name { get; set; } 
    public bool enabled { get; set; } 
    [ScaffoldColumn(false)] 
    public string mapLocationItemType { get; set; } 
    [ScaffoldColumn(false)] 
    public string userName { get; set; } 
    [ScaffoldColumn(false)] 
    public DateTime creationDate { get; set; } 
    [ScaffoldColumn(false)] 
    public DateTime lastUpdate { get; set; } 
    public string thumbnailDisplayText { get; set; } 
    public bool parentIsMapLocation { get; set; } 
    public int thumbnailAssetId { get; set; } 
    public virtual Asset thumbnailAsset { get; set; } 
    public int mapLocationId { get; set; } 
    public virtual MapLocation mapLocation { get; set; } 
    public int mapLocationListGalleryId { get; set; } 
    public virtual MapLocationListGallery mapLocationListGallery { get; set; } 

    public virtual ICollection<MapLocationListItem> listItems { get; set; } 

    public MapLocationList() 
    { 
     creationDate = DateTime.Now; 
     lastUpdate = DateTime.Now; 
     listItems = new List<MapLocationListItem>(); 
    } 
} 

나는 또한하여 OnModelCreating 기능에 다음과 같은 한 :

여기 내 모델입니다. 아마 당신은 낙타 표기법에 속성의 이름을 변경 (자본 M주의)해야합니다

public int MapLocationListGalleryId { get; set; } 
public virtual MapLocationListGallery MapLocationListGallery { get; set; } 

을 즉 ForeignKeyAttributeherehere 살펴보고 도움이되지 않는 경우. 더 많은 정보는 this article를 참조하십시오

modelBuilder.Entity<MapLocationList>() 
    .HasRequired(c => c.mapLocationListGallery) 
    .WithMany() 
    .HasForeignKey(x => x.mapLocationListGalleryId) 
    .WillCascadeOnDelete(false); 

:

편집

가 나는 유창 API에 익숙하지 해요,하지만 난 당신이 뭔가를 사용하여 명시 적으로 외래 키를 설정하려고 수 있다고 생각 주제 : "비정상적인 외부 키 이름 구성". 코드가 코드 첫 번째 규칙 (대문자 M, 즉 클래스 이름 포함)을 준수하는 것으로 보이기 때문에 이는 이상합니다.

+0

내 게시물에 몇 가지 추가 정보를 추가했습니다. – user3736648

관련 문제