在這里,我嘗試在 VS 2017 中開發(fā) .NetFrameworkCore 項目。我有一個包含兩個項目的解決方案類庫ASP .net 核心應(yīng)用程序我想創(chuàng)建可以使用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"); }對于我使用過的關(guān)鍵注釋System.ComponentModel.DataAnnotations這是因為我還沒有找到System.Data.Entity.Core. 編譯解決方案工作正常,但在創(chuàng)建遷移時我有錯誤說明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.我想,有兩種可能我正在使用 ASP.net 核心應(yīng)用程序,其中我有 ClassLibrary 項目,因此我遇到了上述錯誤遷移時,它正在搜索System.Data.Entity.CoreEF6我應(yīng)該如何使用Code First Migrations.
1 回答

Smart貓小萌
TA貢獻1911條經(jīng)驗 獲得超7個贊
您的代碼中似乎沒有任何問題。我建議您使用 EF 6 中的 Fluent API 配置在EfConfiguration. 試試這個。
private static void EfConfiguration(ModelBuilder modelBuilder)
{
modelBuilder.Entity<utblPhoto>().ToTable("utblPhoto");
modelBuilder.Entity<utblPhoto>().HasKey(t=> new {t.PhotoId});
}
完整指南可在此處找到。希望這對你有幫助。
- 1 回答
- 0 關(guān)注
- 218 瀏覽
添加回答
舉報
0/150
提交
取消