【问题标题】:Could not load file or assembly 'System.ComponentModel.DataAnnotations' in Visual Studio 2017 while doing Code First Migrations执行 Code First 迁移时无法在 Visual Studio 2017 中加载文件或程序集“System.ComponentModel.DataAnnotations”
【发布时间】:2018-07-04 05:44:10
【问题描述】:

这里我正在尝试在 VS 2017 中开发 .NetFrameworkCore 项目。我有一个包含两个项目的解决方案

  1. 类库
  2. ASP .net 核心应用程序

我想创建可以使用enable-migrationsadd-migration InitialCreate 的迁移。在此之前,我在 startUp.cs 中配置了我的项目,如下所示:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        //======================================Setting ConnectionString to Database
        services.AddDbContext<EfDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DbCon")));
        //services.AddCors();
        //======================API Level and Function Level
        services.AddCors(action => action.AddPolicy("PolicyName", //============Adding Policy which was Created in the api 
            builder => builder.WithOrigins("http://localhost:4200") //Adding access the URL for giving access to the URL(s)
            .WithMethods("Get", "Post", "Put", "Delete")// Adding access to the method(s) for the request from the 'URL'
            .AllowAnyHeader()));//Adding access to any headers from the 'URL'

    }

这绝对没问题。我喜欢这样的 EfDbContext:

    public EfDbContext(DbContextOptions options) : base(options)
    {
    }
    public DbSet<utblPhoto> utblPhotos { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        EfConfiguration(modelBuilder);
    }
    private static void EfConfiguration(ModelBuilder modelBuilder)
    {
       //**************************************Table Mapping 
       modelBuilder.Entity<utblPhoto>().ToTable("utblPhoto");
    }

我的 utblPhoto 班级在 ClassLibrary 项目中,我有这样的:

public class utblPhoto
{
    [Key]
    public int PhotoId { get; set; }
    public string PhotoTitle { get; set; }
    public string PhotoDescription { get; set; }
    public string PhotoUrl { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedOn { get; set; }
    public string UpdatedBy { get; set; }
}

对于关键注解我使用了System.ComponentModel.DataAnnotations 这是因为我没有找到System.Data.Entity.Core。编译解决方案工作正常,但在创建迁移时我有错误声明 Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

我猜,有两种可能

  1. 我正在使用 ASP.net 核心应用程序,其中我有 ClassLibrary 项目,因此我遇到了上述错误
  2. 迁移时,它正在为 EF6 搜索 System.Data.Entity.Core

我应该如何使用Code First Migrations 在 SQL Server 中迁移我的 EfDbClasses。

【问题讨论】:

标签: c# asp.net-core entity-framework-6 entity-framework-core asp.net-core-webapi


【解决方案1】:

您的代码似乎没有错。我建议您在EfConfiguration 中使用 EF 6 中的 Fluent API 配置来配置您的模型。试试这个。

private static void EfConfiguration(ModelBuilder modelBuilder)
{
   modelBuilder.Entity<utblPhoto>().ToTable("utblPhoto");
   modelBuilder.Entity<utblPhoto>().HasKey(t=> new {t.PhotoId});
}

完整的指南可以在here 找到。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2019-02-01
    • 2014-02-12
    • 2018-03-23
    • 2017-02-01
    • 1970-01-01
    相关资源
    最近更新 更多