0

I want to use entity framework to extend the functionality of Sitecore's membership. As you know, you can't query data efficiently like you can with an SQL database.

I am trying to setup a context class using Code first migrations

public class FilesContext : DbContext
{
    public FilesContext() : base("core")
    {
        Database.CreateIfNotExists();
    }

    public DbSet<File> Files{ get; set; }
}

The problem is that, the Core database doesn't "exist", when I run the Update-Database. However when I call

ConfigurationManager.ConnectionStrings;

I get a list of the connection strings listed in https://doc.sitecore.net/sitecore_experience_platform/81/setting_up_and_maintaining/xdb/configuring_servers/database_connection_strings_for_configuring_servers

and I am wondering why Entity Framework isn't able to pick up the Sitecore databases. I think, maybe when I run the Update-Database command in Package Manager Console, it just tries to invoke a call to the FilesContext class and it doesn't see the bigger picture, which is that the site is running under Sitecore.

6
  • what version of sitecore you are currently working on?
    – Rathan M
    Commented Feb 26, 2018 at 14:12
  • @RathanM Sitecore.NET 8.2 (rev. 170614) Commented Feb 26, 2018 at 15:14
  • 5
    If you don't mind me asking, what are you ultimately trying to do? My first impression would be to say do not in anyway alter or update the Core db schema as this has major implications and there are likely many other ways to achieve what you might be trying to do that won't gravely affect your ability to upgrade in the future, but before I respond with that, I would like to know what you are trying to do.
    – vandsh
    Commented Feb 26, 2018 at 16:52
  • I am not sure what you are trying to do but i would not recommend using Entity Framework or altering the core database. Commented Feb 26, 2018 at 17:28
  • @vandsh What I am trying to do is; Search for users in implement a account verification feature using Sitecore's User Profile. As you know, you can call Membership.GetAllUser() and search for something, but when you have millions of records, that becomes expensive. Sitecore User Profile membership doesn't allow you to query data like you can with SQL databases, rather you'd have to know the UserName of the user before you can retrieve any of the custom properties associated with their profile. When a user registers an account, they're sent a verification token Commented Feb 27, 2018 at 10:32

1 Answer 1

1

I agree with the comments about not using EF CodeFirst approach with any of the default Sitecore databases. I would suggest using a custom database or only using the core database to query data from existing tables (using CodeFirst based on an existing database). Be careful about any changes you make to any of the default Sitecore databases and tables.

That said, when you run the Update-Database command from the Package Manager console you are doing so from the context of Visual Studio and the selected project from the console window. When you are calling ConfigurationManager.ConnectionStrings to get a list of the connection strings I presume you are running this in your website deployed to IIS. EF will not run in the context of the website so does not have access to those.

Assuming you have a separate Class project for your EF project, you need to add the connection string in the App.config of that project and then make sure you build the project since the config will get copied to the /bin folder of that project.

<connectionStrings>
  <add name="core" 
       providerName="System.Data.SqlClient" 
       connectionString="Data Source=(local);Database=Sitecore_core;..." />
</connectionStrings>

You must add the connection string to App.config of the EF project even if it is specified elsewhere (such as in your Web project). Make sure you add the providerName attribute the connection string, you will get an error when using EF without it.

2
  • When you say App.config, do you mean web.config? There are multiple projects in our solution, we're currently using the Helix approach from Sitecore. I've tried adding the connection strings in the web.config in the EF project but it doesn't seem to work. I get the dreaded, "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify......." Commented Feb 28, 2018 at 17:13
  • Yes, it should work just the same for web.config. Make sure you have added EF config sections. I'm currently using EF in a Helix project without any issues (custom database). You can also try with the -Verbose switch to additional logging. Also check if the config is copied to your /bin folder (same name next to the DLL) - this is what is used by the Update-Database command.
    – jammykam
    Commented Feb 28, 2018 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.