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

asp.net-core asp.net-core-webapi c# entity-framework-6 entity-framework-core

Question

Here I am trying to develop .NetFrameworkCore project in VS 2017. I have a solution with two projects

  1. Class Library
  2. ASP .net Core Application

I wanted to create migration for which I can use enable-migrations and add-migration InitialCreate. Before that I have configure my project in startUp.cs like this:

    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'

    }

Which is absolutely fine. I heve EfDbContext like this:

    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");
    }

My utblPhoto class is in ClassLibrary Project where I have like this:

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; }
}

For key annotation I have used System.ComponentModel.DataAnnotations This is because I have not found System.Data.Entity.Core. Compiling the solution works fine but while creating the migration I am having error stating 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.

I guess, there are two possibilities

  1. I am using ASP.net core application in which I have ClassLibrary Project, because of which I am having the said error
  2. While migrating, it is searching for System.Data.Entity.Core for EF6

How should I migrate my EfDbClasses in SQL Server using Code First Migrations.

1
0
7/4/2018 5:44:10 AM

Accepted Answer

Seems nothing wrong in your code. I suggest you to configure your model using Fluent API Configurations in EF 6 in your EfConfiguration. Just try this.

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

Complete guide can be found here. Hope this helps you.

1
7/4/2018 6:11:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow