添加代码生成项时,EF DB首先生成错误

时间:2016-05-01 13:12:27

标签: c# entity-framework visual-studio-2015

我正在尝试为我的edmx运行代码生成,但每次我这样做时,我都会遇到很多错误。

这是生成的Context.cs文件:

//------------------------------------------------------------------------------
/ <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace project.DAL
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

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

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

        public virtual DbSet<Commodity> Commodities { get; set; }
        public virtual DbSet<CommoditiesPrice> CommoditiesPrices { get; set; }

     }
}

我得到的错误:

Severity    Code    Description Project File    Line
Error   CS0426  The type name 'Data' does not exist in the type 'System'    project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   13
Error   CS0426  The type name 'Data' does not exist in the type 'System'    project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   14
Error   CS0246  The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?) project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   16
Error   CS0115  'NebulabEntities.OnModelCreating(DbModelBuilder)': no suitable method found to override project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   23
Error   CS0246  The type or namespace name 'DbModelBuilder' could not be found (are you missing a using directive or an assembly reference?)    project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   23
Error   CS0246  The type or namespace name 'DbSet<Commodity>' could not be found (are you missing a using directive or an assembly reference?)  project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   28
Error   CS0246  The type or namespace name 'DbSet<CommoditiesPrice>' could not be found (are you missing a using directive or an assembly reference?)   project.DAL C:\Test\project.DAL\RedSpiderDataModel.Context.cs   29

我正在使用Visual Studio 2015,我已将EF添加到我的类库项目中。

有谁知道为什么它会生成错误的代码?

1 个答案:

答案 0 :(得分:0)

这是因为您在项目中引用了较旧的实体框架dll。解决方案是 - 1.首先从Reference Manager中删除旧EF dll的引用。 2.然后使用浏览器功能添加较新的EF dll。

对于EF 6.1.3,您将获得文件夹“EntityFramework.6.1.3 \ lib \ net45”中的dll