3

我得到一个可能很容易解决的常见错误,但我不知道如何解决。这是我得到的信息。

列名无效。[节点名称(如果有)= Extent1,列名称= Blog_BlogId]

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: 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; }
}
4

3 回答 3

1

考虑更改您的 Post 类,以便 BlogId 将 FK 引用到关联的 Blog 而不是 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; }
}
于 2012-07-02T00:04:49.520 回答
1

你可以试试... ForeignKey注解

公共虚拟 int BlogId { 获取;放; }

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

于 2016-02-15T18:50:06.437 回答
0

从数据库中分离连接,删除与应用程序相关的任何应用程序数据并重建并重试,它对我有用。

于 2015-01-06T05:25:33.110 回答