该类型不归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中

ror*_*yok 2 c# sql entity-framework asp.net-mvc-4

我在我的MVC4应用程序的其中一个页面上收到此错误

The type 'Group' is not attributed with EdmEntityTypeAttribute but is 
contained in an assembly attributed with EdmSchemaAttribute. POCO 
entities that do not use EdmEntityTypeAttribute cannot be contained 
in the same assembly as non-POCO entities that use EdmEntityTypeAttribute.
Run Code Online (Sandbox Code Playgroud)

一分钟它工作正常,下一个这个错误出现了,不会消失.我没有改变与Context Model相关的任何内容.如果我回滚所有其他代码更改,则错误将继续.

我已经看过这篇文章,它处理了类似的错误.但是我整个都在使用DbContext.该修复程序对我不起作用.

我已经尝试重新生成类,甚至删除并重新创建.edmx,没有什么对我有用.

这是一个非工作类的代码的一部分

该表(从SQL服务器提取的SQL create语句):

CREATE TABLE [dbo].[Groups](
    [group_id] [int] IDENTITY(1,1) NOT NULL,
    [user_id] [uniqueidentifier] NULL,
    [parent_group_id] [int] NULL,
    [group_type] [tinyint] NULL,
    [group_name] [nvarchar](50) NULL,
    [date_created] [smalldatetime] NULL,
    [date_accessed] [smalldatetime] NULL,
    [date_modified] [smalldatetime] NULL,
    [date_deleted] [smalldatetime] NULL,
    [n_total_contacts] [int] NULL,
    [n_unsubscribed] [int] NULL,
    [n_excluded] [int] NULL,
    CONSTRAINT [PK_Group] PRIMARY KEY CLUSTERED 
    (
        [group_id] ASC
    )
    WITH 
    (
        PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
        ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON
    ) 
    ON [PRIMARY]
) 
ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

自动生成的上下文类:

namespace MyProject
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Linq;

    public partial class MyDbContext : DbContext
    {
        public MyDbContext () : base("name=MyDbContext ")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Group> Groups { get; set; }

    }
}
Run Code Online (Sandbox Code Playgroud)

自动生成的Group.cs类:

namespace MyProject
{
    using System;
    using System.Collections.Generic;

    public partial class Group
    {
        public int group_id { get; set; }
        public Nullable<System.Guid> user_id { get; set; }
        public Nullable<int> parent_group_id { get; set; }
        public Nullable<byte> group_type { get; set; }
        public string group_name { get; set; }
        public Nullable<System.DateTime> date_created { get; set; }
        public Nullable<System.DateTime> date_accessed { get; set; }
        public Nullable<System.DateTime> date_modified { get; set; }
        public Nullable<System.DateTime> date_deleted { get; set; }
        public Nullable<int> n_total_contacts { get; set; }
        public Nullable<int> n_unsubscribed { get; set; }
        public Nullable<int> n_excluded { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

View模型:

public class GroupsListModel
{
    public List<Group> Groups { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

风景:

public ActionResult List() 
{
    GroupsListModel model = new GroupsListModel();
    using (MyDbContext db = new MyDbContext())
    {
        model.Groups = db.Groups.ToList();
    }
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

再说一次,我只想补充一点,这一切都在15分钟前完成.我在视图中更改了几行导致此错误,当我将其更改回来时,错误仍然存​​在.

**更新:**

我已经接受了@ Pawel的答案,因为它解决了原始问题,但我只想分享更多有关完整解决方案的信息

在这个项目中,我连接到两个服务器,其中一个运行Sql Server 2005,另一个运行Sql Server 2008.删除EdmSchemaAttribute后,我收到一个新错误:

All SSDL artifacts must target the same provider. The ProviderManifestToken ’2008? is different from ’2005? that was encountered earlier.

我用Google搜索并找到了我应该编辑2005 .edmx文件并将2005更改为2008的建议.这导致了一个新错误: The version of SQL Server in use does not support datatype ‘datetime2?

事实证明,datetime我的SQL Server 2008数据库中的一date列实际上是一列.对于其他一切,这没有任何区别,但date映射到datetime2EF,这导致2005模式的问题.我相信这可能是问题的根本原因.

我使用的解决方案是编辑我的2008 .edmx文件,将2008更改为2005并将date字段更改为a datetime.在此之后,该项目编译并运行没有问题.

Paw*_*wel 6

在EF支持的POCO实体之前,它仅支持非POCO实体.非POCO实体派生自EntityObject类,每个实体,复杂类型,枚举类型,成员等必须归因于EF特定属性,否则它们将无法工作.非POCO实体居住的集会必须归功于EdmSchemaAttribute.如果程序集具有此属性,则EF知道它包含非POCO属性,并且仅查找这些属性.POCO和非POCO类型不能存在于同一个程序集中.在你的情况下,你似乎有EdmSchemaAttribute在项目的某个地方定义(请注意,这是一个程序集级属性,因此它基本上可以存在于任何文件中).如果您只想使用POCO类型,只需找到该属性并将其删除即可.如果要混合POCO和非POCO类型,则需要在不同的程序集中定义POCO类型,而不是定义非POCO类型(我不建议混合使用POCO和非POCO类型).请注意,在EF Designer中,非POCO类型曾经是VS2010(基于EntityObject的Entities和ObjectContext的上下文)中的默认选择.在VS2012附带的设计器中,默认上下文是DbContext,但您仍然可以将代码生成策略更改为"默认"(有趣的是,默认的代码生成策略创建基于DbContext的上下文,POCO实体称为"无").EdmSchemAttribute.在VS2013(和VS2012的OOB版本)中,如果你的目标是EF6,你将无法选择代码生成策略(一切都是基于T4的 - 如果你真的需要ObjectContext,你应该能够在VS Gallery上找到模板).如果你的目标是EF5,你可以选择策略来生成非POCO的东西,但我认为它被称为Legacy ObjectContext而不是T4,它是DbContext和朋友.