1

I have an application that includes a Web API Core startup project. I found that I cannot scaffold an existing database in .Net Core for a View. So I decided for my Web API Core application that I will have to use the .Net framework and get the data in a project using EF6. However in the startup project (Web API Core) I need to set the connection string. For this I need EF6, and it would seem that I cannot do this even if I add EF6 in the project (maybe this can't be done in a Core project?). So this code fails where I attempt to add the context;

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore()
            .AddMvcOptions(o => o.OutputFormatters.Add(
                new XmlDataContractSerializerOutputFormatter()));
        var connectionString = Startup.Configuration["connectionStrings:propertiesConnectionString"];
        services.AddDbContext<SurveyEntities>(o => o.UseSqlServer(connectionString));

        services.AddScoped<IPropertiesRepo, PropertiesRepo>();

    }

If I download EF6, I get this error;

Error CS0311 The type 'Properties.EF6.MSurveyV2Entities' cannot be used as type parameter 'TContext' in the generic type or method 'EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, Action, ServiceLifetime)'. There is no implicit reference conversion from 'Properties.EF6.MSurveyV2Entities' to 'Microsoft.EntityFrameworkCore.DbContext'.

So how do I fix this?

3
  • 1
    What error do you get? Commented Apr 25, 2017 at 8:43
  • Error CS0311 The type 'Properties.EF6.MSurveyV2Entities' cannot be used as type parameter 'TContext' in the generic type or method 'EntityFrameworkServiceCollectionExtensions.AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)'. There is no implicit reference conversion from 'Properties.EF6.MSurveyV2Entities' to 'Microsoft.EntityFrameworkCore.DbContext'. Commented Apr 25, 2017 at 8:49
  • It would seem that a Core API cannot set a connection string for a EF6 project. Commented Apr 25, 2017 at 8:52

2 Answers 2

2

I found the answer in this link here. https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6 If the link breaks this is a summary; In the EF6 project I put in a partial class for the db context where a constructor takes the connection string as a parameter public partial class SurveyEntities

{
    public SurveyEntities(string connString) : base(connString)
    {
    }
}

In the Web.API core project I have in the startup;

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore();
        var connectionString = Configuration.GetConnectionString("SurveyEntities");
        services.AddScoped<SurveyEntities>(_ => new SurveyEntities(connectionString));
        services.AddScoped<IPropertiesRepo, PropertiesRepo>();

    }

and in the controller I have

   public class ContractController : Controller
    {
        private readonly IPropertiesRepo PropertiesRepo;
        private readonly SurveyEntities db;
        public ContractController(SurveyEntities _db,IPropertiesRepo repo)
        {
            this.db = _db;
            this.PropertiesRepo = repo;
        }

        [HttpGet("getall")]
        public IActionResult GetContracts()
        {
            var contracts = this.PropertiesRepo.GetAllLiveContracts().ToList();
            return Ok(contracts);
        }
    }

The call to the api now works.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use the connection string stored in appsetings.json. This is the way you have the most flexiblity configuring ASP.NET Core.

i.e.

{
  "ConnectionStrings": {
      "MyDatabase": "Server (localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
   },
}

After that in configure services

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options =>    
    options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));
}

You have more info and examples here https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.