模式无效,无法加载类型,因为程序集包含EdmSchemaAttribute [英] Schema invalid and types cannot be loaded because the assembly contains EdmSchemaAttribute

查看:141
本文介绍了模式无效,无法加载类型,因为程序集包含EdmSchemaAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取以下错误:

指定的模式无效。错误:

Schema specified is not valid. Errors:


程序集x,Version = 1.0.0.0,Culture = neutral,
中的类型PublicKeyToken = null'无法加载,因为程序集包含
EdmSchemaAttribute,并且类型的关闭由
名称加载。不能使用名称和属性加载。

The types in the assembly 'x, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be loaded because the assembly contains the EdmSchemaAttribute, and the closure of types is being loaded by name. Loading by both name and attribute is not allowed.

这个错误是什么意思?
我正在尝试从现有的数据库中将我的应用程序的鞋子编号转换为EF模型。
在此应用程序基于CodeFirst之前,并使用存储库模式,但对于我的生活,我无法得到这个工作。

What does this error mean exactly? I'm trying to shoe-horn into my application an EF model from an existing database. Before this application was based on CodeFirst and using the repository pattern but for the life of me I can't get this working.

之前我有: / p>

Before I had:

public class BaseModelContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
}

但是,在EF模型中, db),我不得不删除这些,因为它似乎不喜欢在DbSet属性上有一个存储库模式。
所以我删除了这些,然后存储库可以在已经定义在.designer.cs上下文类(EF模型)上的类上使用存储库。这在生成的代码中设置了EdmSchemaAttribute。

But in a EF model-first scenario (one where tables already exist in the db), I had to remove these as it didn't seem to like having a repository pattern on DbSet properties. So I stripped these out, and the repository can then use repository on the classes already defined on the .designer.cs context class (the EF model). This has the EdmSchemaAttribute set inside the generated code.

那么如何让我的存储库模式在模型第一种情况下工作?

So how do I get my repository pattern to work in the model-first scenario? What does the above error mean exactly?

编辑

添加新代码:

 public class BaseModelContext : DbContext
    {
        // public DbSet<Location> Locations { get; set; }

        public BaseModelContext(string nameOrConnection)
            : base(nameOrConnection)
        {
        }

        public BaseModelContext()
        {
        }
    }

    public class VisitoriDataContext : BaseModelContext
    {
        public VisitoriDataContext()
            : base("visitoriDataConnection")
        {

        }

    }


    public interface IVisitoriDataContextProvider
    {
        VisitoriDataContext DataContext { get; }
    }

    public class VisitoriDataContextProvider : IVisitoriDataContextProvider
    {
        public VisitoriDataContext DataContext { get; private set; }

        public VisitoriDataContextProvider()
        {
            DataContext = new VisitoriDataContext();
        }
    }


    public class VisitoriRepository<T> : IRepository<T> where T : class
    {
        protected readonly IVisitoriDataContextProvider _ctx;

        public VisitoriRepository(IVisitoriDataContextProvider ctx)
        {
            _ctx = ctx;
        }

        public T Get(int id)
        {
            return _ctx.DataContext.Set<T>().Find(id);
        }

    }

    public interface ILocationRepo : IRepository<Location>
    {
        IEnumerable<Location> GetSuggestedLocationsByPrefix(string searchPrefix);
    }

    public class LocationRepo : VisitoriRepository<Location>, ILocationRepo
    {
        public LocationRepo(IVisitoriDataContextProvider ctx)
            : base(ctx)
        {

        }

        public IEnumerable<Location> GetSuggestedLocationsByPrefix(string searchPrefix)
        {

            return Where(l => l.name.Contains(searchPrefix)).ToList();

        }
    }


推荐答案

错误意味着您不能将具有相同名称的实体的代码首映射(数据注释和流畅API)和EDMX映射(使用EntityObjects!)组合起来。这两种方法是分离的。

The error means that you cannot combine code first mapping (data annotations and fluent API) and EDMX mapping (with EntityObjects!) for entity with the same name. These two approaches are disjunctive.

您的其他问题不清楚。

The rest of your question is not clear.

Btw。现有数据库的构建映射称为数据库首先不是首先模型。

Btw. building mapping from existing database is called database first not model first.

这篇关于模式无效,无法加载类型,因为程序集包含EdmSchemaAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆