[英]Could not load file or assembly 'System.ComponentModel.DataAnnotations' in Visual Studio 2017 while doing Code First Migrations
在這里,我正在嘗試在VS 2017中開發.NetFrameworkCore項目。我有兩個項目的解決方案
我想創建可以使用enable-migrations
和add-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.
我猜有兩種可能性
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.