在这里,我尝试在 VS 2017 中开发 .NetFrameworkCore 项目。我有一个包含两个项目的解决方案
类库
ASP .net 核心应用程序
我想创建可以使用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");
}
对于我使用过的关键注释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.
我想,有两种可能
我正在使用 ASP.net 核心应用程序,其中我有 ClassLibrary 项目,因此我遇到了上述错误
迁移时,它正在搜索System.Data.Entity.CoreEF6
我应该如何使用Code First Migrations.
Smart猫小萌
相关分类