Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 列名无效_Entity Framework_Entity Framework 4_Ef Code First - Fatal编程技术网

Entity framework 列名无效

Entity framework 列名无效,entity-framework,entity-framework-4,ef-code-first,Entity Framework,Entity Framework 4,Ef Code First,我有一个常见的错误,可能很容易解决,但我不知道如何解决。这就是我得到的信息 列名无效。[节点名称(如果有)=扩展1,列名=Blog\u BlogId] 说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.Data.SqlServerCe.SqlCeException:列名无效。[节点名称(如果有)=扩展1,列名=Blog\u BlogId] 这是表示实体的类: public class BlogContext

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

列名无效。[节点名称(如果有)=扩展1,列名=Blog\u BlogId]

说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.Data.SqlServerCe.SqlCeException:列名无效。[节点名称(如果有)=扩展1,列名=Blog\u 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; }
}
公共类BlogContext:DbContext
{
公共博客上下文()
:base(“SqlCeServices”)
{
}
公共数据库集用户{get;set;}
公共数据库集博客{get;set;}
公共DbSet Posts{get;set;}
公共数据库集类别{get;set;}
公共DbSet标记{get;set;}
}
[表(“aspnet_用户”)]
公共类用户
{
[必需]
公共Guid应用程序ID{get;set;}
[必需]
公共Guid用户标识{get;set;}
[必需]
公共字符串用户名{get;set;}
[必需]
公共字符串LoweredUserName{get;set;}
公共字符串MobileAlias{get;set;}
[必需]
公共布尔是非对称的{get;set;}
[必需]
公共日期时间LastActivityDate{get;set;}
}
公共类博客
{
[关键]
public int BlogId{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串Url{get;set;}
公共整数评级{get;set;}
公共虚拟ICollection编辑器{get;set;}
公共虚拟ICollection Posts{get;set;}
}
公营职位
{
[关键]
公共int PostId{get;set;}
[必需,最大长度(200)]
公共字符串标题{get;set;}
公共字符串内容{get;set;}
公共字符串抽象{get;set;}
public DateTime DateCreated{get;set;}
公共DateTime DateLastEdited{get;set;}
公共虚拟用户用户ID{get;set;}
公共虚拟ICollection类别{get;set;}
公共虚拟ICollection标记{get;set;}
公共虚拟博客BlogId{get;set;}
}
公共类类别
{
[关键]
public int CategoryId{get;set;}
[必需,最大长度(200)]
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection Posts{get;set;}
公共虚拟类别ParentCategory{get;set;}
公共虚拟博客BlogId{get;set;}
}
公共类标签
{
[关键]
public int TagId{get;set;}
[必需,最大长度(200)]
公共字符串标题{get;set;}
公共虚拟博客BlogId{get;set;}
}

考虑更改您的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; }
}
公共类职位
{
[关键]
公共int PostId{get;set;}
[必需,最大长度(200)]
公共字符串标题{get;set;}
公共字符串内容{get;set;}
公共字符串抽象{get;set;}
public DateTime DateCreated{get;set;}
公共DateTime DateLastEdited{get;set;}
公共虚拟用户用户ID{get;set;}
公共虚拟ICollection类别{get;set;}
公共虚拟ICollection标记{get;set;}
公共虚拟int BlogId{get;set;}
公共虚拟博客{get;set;}
}

从数据库断开连接,删除与应用程序相关的所有应用程序数据,然后重新生成并重试,我成功了。

您可以尝试…ForeignKey注释

公共虚拟int BlogId{get;set;}

[ForeignKey(“BlogId”)]
公共虚拟博客Blog{get;set;}

何时出现异常?什么查询?它通过BlogContext访问用户并将集合强制转换为列表。这是在标准控制器scaffold的索引操作中完成的。所谓“cast”,实际上是指我使用的是ToList()。我认为,首先使用EF代码和Blog>Post>Blog之间的循环依赖关系时,您会遇到问题。。。等等。我怀疑如果您删除了对
Post
对象上的
Blog
属性的引用,问题就会消失。您是否对现有数据库使用EF CF?我有一个类似的问题,通过添加虚拟int解决了这个问题。谢谢