The column name is not valid

喜欢而已 提交于 2021-01-27 13:42:59

问题


I get a common error that probably is easy to solve but I have no clue how. This is the message I get.

The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ]

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ]

This is my classes representing my entities:

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; }
}

回答1:


Consider changing your Post class so that BlogId references the FK to the associated Blog instead of the Blog object itself. Add a new property Blog to reference the actual Blog object.

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; }
}



回答2:


You can try...ForeignKey annotation

public virtual int BlogId { get; set; }

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




回答3:


Detach the connection from DB, Delete any App Data related to application and rebuild and try again, It worked for me.



来源:https://stackoverflow.com/questions/11269892/the-column-name-is-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!