2012-06-29 4 views
3

아마도 쉽게 해결할 수있는 공통적 인 오류가 발생합니다. 단서가 없습니다. 이것은 내가받는 메시지입니다.열 이름이 유효하지 않습니다.

열 이름이 잘못되었습니다. [노드 이름 (있는 경우) = Extent1, 열 이름 = Blog_BlogId]

설명 : 현재 웹 요청을 실행하는 동안 처리되지 않은 예외가 발생했습니다. 오류 및 코드에서 시작된 위치에 대한 자세한 정보는 스택 추적을 검토하십시오.

예외 정보 : System.Data.SqlServerCe.SqlCeException : 열 이름이 유효하지 않습니다. [노드 이름 (있는 경우) = Extent1, 열 이름 = Blog_BlogId]

이 내 엔티티를 나타내는 내 수업입니다 :

public class BlogContext : DbContext 
{ 
    public BlogContext() 
     : base("SqlCeServices") 
    { 

    } 

    public DbSet<User> Users { get; set; } 

    public DbSet<Blog> Blogs { get; set; } 

    public DbSet<Post> Posts { get; set; } 

    public DbSet<Category> Categories { get; set; } 

    public DbSet<Tag> Tags { get; set; } 
} 

[Table("aspnet_Users")] 
public class User 
{ 
    [Required] 
    public Guid ApplicationId { get; set; } 

    [Required] 
    public Guid UserId { get; set; } 

    [Required] 
    public string UserName { get; set; } 

    [Required] 
    public string LoweredUserName { get; set; } 

    public string MobileAlias { get; set; } 

    [Required] 
    public bool IsAnonymous { get; set; } 

    [Required] 
    public DateTime LastActivityDate { get; set; } 
} 

public class Blog 
{ 
    [Key] 
    public int BlogId { get; set; } 

    [Required] 
    public string Name { get; set; } 
    public string Url { get; set; } 
    public int Rating { get; set; } 

    public virtual ICollection<User> Editors { get; set; } 

    public virtual ICollection<Post> Posts { get; set; } 
} 

public class Post 
{ 
    [Key] 
    public int PostId { get; set; } 
    [Required, MaxLength(200)] 
    public string Title { get; set; } 
    public string Content { get; set; } 
    public string Abstract { get; set; } 
    public DateTime DateCreated { get; set; } 
    public DateTime DateLastEdited { get; set; } 

    public virtual User UserId { get; set; } 

    public virtual ICollection<Category> Categories { get; set; } 
    public virtual ICollection<Tag> Tags { get; set; } 

    public virtual Blog BlogId { get; set; } 
} 

public class Category 
{ 
    [Key] 
    public int CategoryId { get; set; } 
    [Required, MaxLength(200)] 
    public string Title { get; set; } 
    public string Description { get; set; } 

    public virtual ICollection<Post> Posts { get; set; } 

    public virtual Category ParentCategory { get; set; } 

    public virtual Blog BlogId { get; set; } 
} 

public class Tag 
{ 
    [Key] 
    public int TagId { get; set; } 
    [Required, MaxLength(200)] 
    public string Title { get; set; } 

    public virtual Blog BlogId { get; set; } 
} 
+0

언제 예외가 발생합니까? 어떤 쿼리? –

+0

BlogContext를 통해 사용자에게 액세스하고 컬렉션을 목록으로 변환합니다. 이것은 표준 컨트롤러 스캐 폴드의 Index 액션에서 수행됩니다. –

+0

"캐스트"는 실제로 ToList()를 사용하고 있음을 의미합니다. –

답변

1

BlogId 대신 관련 블로그에 FK를 참조 할 수 있도록 귀하의 게시물 클래스를 변경하는 것이 좋습니다 Blog 객체 자체의 실제 Blog 개체를 참조 할 수있는 새로운 속성 Blog를 추가하십시오.

public class Post 
{ 
    [Key] 
    public int PostId { get; set; } 
    [Required, MaxLength(200)] 
    public string Title { get; set; } 
    public string Content { get; set; } 
    public string Abstract { get; set; } 
    public DateTime DateCreated { get; set; } 
    public DateTime DateLastEdited { get; set; } 

    public virtual User UserId { get; set; } 

    public virtual ICollection<Category> Categories { get; set; } 
    public virtual ICollection<Tag> Tags { get; set; } 

    public virtual int BlogId { get; set; } 
    public virtual Blog Blog { get; set; } 
} 
+0

나는이 문제를 해결했고 가상 int를 추가하여이 문제를 해결했다. 감사! – Sam

0

응용 프로그램과 관련된 응용 프로그램 데이터를 삭제하고 다시 작성하고 다시 시도하십시오. DB에서 연결을 분리하고 다시 시도하십시오.

1

당신은 시도 할 수 있습니다 ... 외래 키 주석

public 가상 INT BlogId {얻을; 세트; }

[ForeignKey ("BlogId")] public virtual Blog Blog {get; 세트; }

관련 문제