簡體   English   中英

執行代碼優先遷移時無法在Visual Studio 2017中加載文件或程序集'System.ComponentModel.DataAnnotations'

[英]Could not load file or assembly 'System.ComponentModel.DataAnnotations' in Visual Studio 2017 while doing Code First Migrations

在這里,我正在嘗試在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 Project中,我這樣子:

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. 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. 我正在使用具有ClassLibrary Project的ASP.net核心應用程序,因此我遇到了上述錯誤
  2. 遷移時,它將搜索EF6的System.Data.Entity.Core

如何使用Code First Migrations功能在SQL Server中遷移EfDbClasses。

您的代碼似乎沒什么問題。 建議您使用EfConfiguration中EF 6中的Fluent API配置來配置模型。 嘗試一下。

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

完整的指南可以在這里找到。 希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM