【发布时间】:2018-07-04 05:44:10
【问题描述】:
这里我正在尝试在 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");
}
我的 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.
我猜,有两种可能
- 我正在使用 ASP.net 核心应用程序,其中我有 ClassLibrary 项目,因此我遇到了上述错误
- 迁移时,它正在为 EF6 搜索
System.Data.Entity.Core
我应该如何使用Code First Migrations 在 SQL Server 中迁移我的 EfDbClasses。
【问题讨论】:
-
@PixelDev,是的,我已经尝试清理我的解决方案并重新启动 VS
-
@KarunChettri,如何实现 HasKey() 函数?
标签: c# asp.net-core entity-framework-6 entity-framework-core asp.net-core-webapi