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:
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:
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>();
}
}
}
="1.0"="utf-8"
<configuration>
<configSections>
<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