Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have an application where I'm tinkering with Code First Entity Framework.

So far I do have my Data Context (called ServerContext) up and working nicely.
I tried to use an initalizer (ServerContextInitalization) which looks like this:

C#
using Dl.ProgramCenter.Common.Dataobjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dl.ProgramCenter.Server.Entity.DataInitialization
{
    public class ServerContextInitalization : System.Data.Entity.DropCreateDatabaseIfModelChanges<ServerContext>
    {
        protected override void Seed(ServerContext context)
        {

            var applicationGroups = new List<ApplicationGroup>
            {
                new ApplicationGroup {Name="Example Application Group"  }
            };
            applicationGroups.ForEach(s => context.ApplicationGroups.Add(s));
            context.SaveChanges();

            var applications = new List<Application>
            {
                new FileShareApplication {Name = "Example Application", Description="This is an example Application.", Path="C:\\Example", ApplicationGroupId=1}
            };

            applications.ForEach(s => context.Applications.Add(s));
            context.SaveChanges();
        }
    }
}

ServerContext.cs:
C#
using Dl.ProgramCenter.Common.Dataobjects;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dl.ProgramCenter.Server.Entity
{
    public class ServerContext : DbContext
    {
        public ServerContext() : base("ServerContext") { }
        
        public DbSet<Application> Applications { get; set; }
        public DbSet<ApplicationGroup> ApplicationGroups { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

}


XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <contexts>
      <context type="Dl.ProgramCenter.Server.Entity.ServerContext, Dl.ProgramCenter.Server.Entity.dll">
        <databaseInitializer type="Dl.ProgramCenter.Server.Entity.DataInitialization.ServerContextInitalization, Dl.ProgramCenter.Server.Entity.dll" />
      </context>
    </contexts>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value=".\" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="ServerContext" connectionString="Data Source=.\;Initial Catalog=ProgramCenterDB;Integrated Security=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>


Both the Initializator class and the context class are placed in the same assembly (Dl.ProgramCenter.Server.Entity.dll) - But for some reason the Context class can be loaded at runtime, but it won't find the assembly Dl.ProgramCenter.Server.Entity.dll (raising a corresponding exception and not initializing anything).

What I checked so far:

- Context (which is in the same assembly as the Initialization class) can be found
- The assembly is copied to the ouput directory
- The assembly name in the config file matches with the assembly name in the output directory


Any solutions/suggestions appreciated

cheers,
Marco
Posted
Comments
Nathan Minier 4-Jan-16 7:33am    
I think that:
protected override void Seed(ServerContext context)

Should be:
protected override void Seed(DbContext context)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900