StructureMap and MongoRepository.1.6.2 .Net 4.5 - mongodb

The Problem:
I am having trouble getting StructureMap to find the default instance for the IRepository<> type in the MongoRepostiory namespace.
The exception message:
"StructureMap Exception Code: 202
No Default Instance defined for PluginFamily MongoDB.Driver.MongoUrl, MongoDB.Driver, Version=1.8.3.9, Culture=neutral, PublicKeyToken=f686731cfb9cc103"
It appears that StructureMap is instantiating the MonogoRepository class with the wrong constructor...
The MonogoRepository has the following constructors:
public class MongoRepository<T> : MongoRepository<T, string>, IRepository<T>, IRepository<T, string>, IQueryable<T>, IEnumerable<T>, IQueryable, IEnumerable where T : MongoRepository.IEntity<string>
{
public MongoRepository();
public MongoRepository(MongoUrl url);
public MongoRepository(string connectionString);
public MongoRepository(MongoUrl url, string collectionName);
public MongoRepository(string connectionString, string collectionName);
}
I want the public MongoRepository(string connectionString); constructor to be called...but it appears that from the exception message, since StructureMap is attempting to resolve MongoUrl, it is not calling the desired one.
I wonder:
Questions:
Have I setup something wrong which is causing the incorrect constructor to be called?
or
Is this the default behaviour, and if so, how do I change it to ensure the desired constructor is called?
The Setup:
I have the following Registry class:
public class IocRegistry : Registry
{
public IocRegistry()
{
this.For(typeof(IRepository<>)).Use(typeof(MongoRepository<>))
.CtorDependency<string>("connectionString")
.Is("MongoServerSettings");
}
}
And the following Container initialization:
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.LookForRegistries();
scan.WithDefaultConventions();
});
});
return ObjectFactory.Container;
}
The IocRegistry is definitely getting hit when I put a break-point in its constructor and debug the application.
The code which consumes the IRepository<> is:
public class ImageContentService : IImageContentService
{
private IRepository<ImageItem> imageRepository;
public ImageContentService(IRepository<ImageItem> imageRepository)
{
this.imageRepository = imageRepository;
}
}
The relevant output of ObjectFactory.Container.WhatDoIHave() (once all registrations have taken place) is:
===========================================================================================================
Configuration Sources:
0) Registry: StructureMap.InitializationExpression, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
1) Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
2) Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
3) Registry: Mch.Core.IocRegistry, Mch.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
============================================================================================================================================================================================================================================================================================================================================
PluginType Name Description
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IRepository`1<T> (IRepository`1<T>) d26c76dd-8550-4d02-bf8a-25c6f0075346 Configured Instance of MongoRepository.MongoRepository`1, MongoRepository.Net45, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient
d26c76dd-8550-4d02-bf8a-25c6f0075346 Configured Instance of MongoRepository.MongoRepository`1, MongoRepository.Net45, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null

You can tell StructureMap which constructor to use:
x.SelectConstructor<ClassWithTwoConstructors>(()=>new ClassWithTwoConstructors(0));
You can find this in the StructureMap documentation.

I had the exact same issue. In the end I solved it by configuring my MongoRepository in the ObjectFactory initialisation like so:
x.For<IRepository<T>>().Use(() => new MongoRepository<T>("{ConnectionString}","{CollectionName}"));
This way it seems the Structuremap seems to use the correct constructor. Obviosuly you can instantiate the MongoRepository with whichever constructor you prefer.

Related

DbContext class - ASP.Net.Core

How can I apply the :base("name=connectionstring_name") in ASP.NET Core?
Because my Visual Studio shows cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' .
namespace SchoolDataLayer
{
public class Context: DbContext
{
public SchoolDBContext() : base("name=SchoolDBConnectionString")
{
}
}
}
public SchoolDBContext() : base("name=SchoolDBConnectionString")
As error says you should pass DbContextOptions class instead of connection string.
The DbContextOptions instance carries configuration information such as:
The database provider to use, typically selected by invoking a method such as UseSqlServer or UseSqlite. These extension methods require the corresponding provider package, such as Microsoft.EntityFrameworkCore.SqlServer or Microsoft.EntityFrameworkCore.Sqlite. The methods are defined in the Microsoft.EntityFrameworkCore namespace.
Any necessary connection string or identifier of the database instance, typically passed as an argument to the provider selection method mentioned above
Any provider-level optional behavior selectors, typically also chained inside the call to the provider selection method
Any general EF Core behavior selectors, typically chained after or before the provider selector method
here is an example:
public class Context: SchoolDbContext
{
public SchoolDbContext(DbContextOptions<SchoolDbContext> options)
: base(options)
{
}
}
for more information read https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

Configure custom database initializer in app.config

We have a custom initializer that does not change the database schema in any way; all it does is seed some data.
I've been asked to come up with a custom initializer that will always drop the database and then populate it with the same seed data (this will be used only in our database test projects).
I have written this initializer:
public class DropAndSeedDbInitializer<TContext, TSeedDataInitializer> : IDatabaseInitializer<TContext>
where TContext : DbContext
where TSeedDataInitializer : IDatabaseInitializer<TContext>, new()
{
private readonly DropCreateDatabaseAlways<TContext> _dbInitializer;
private readonly IDatabaseInitializer<TContext> _seedDataInitializer;
public DropAndSeedDbInitializer()
{
_dbInitializer = new DropCreateDatabaseAlways<TContext>();
_seedDataInitializer = Activator.CreateInstance<TSeedDataInitializer>();
}
public void InitializeDatabase(TContext context)
{
context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction
, string.Format("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", context.Database.Connection.Database));
_dbInitializer.InitializeDatabase(context);
_seedDataInitializer.InitializeDatabase(context);
}
}
It works fine when I call
LicenceContext context = new LicenceContext();
Database.SetInitializer(new DropAndSeedDbInitializer<LicenceContext, TestSeedDataInitializer>());
context.Database.Initialize(true);
Then I decided to configure it in app.config like so:
<context type="Foo.LicenceContext, MyAssembly">
<databaseInitializer
type="Foo.DropAndSeedDbInitializer`2[[Foo.LicenceContext, MyAssembly], Foo.TestSeedDataInitializer, MyAssembly]], MyAssembly" />
</context>
and I removed this code:
Database.SetInitializer(new DropAndSeedDbInitializer<LicenceContext, TestSeedDataInitializer>());
context.Database.Initialize(true);
But then I get this error the first time I try to access my context:
Failed to set database initializer of type 'Foo.DropAndSeedDbInitializer`2[[Foo.LicenceContext, MyAssembly], Foo.TestSeedDataInitializer, MyAssembly]], MyAssembly' for DbContext type 'Foo.LicenceContext, MyAssembly' specified in the application configuration. See inner exception for details.
The inner exception is:
Exception of type 'System.ArgumentException' was thrown.
Parameter name: typeName#149
at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage)
at System.Data.Entity.Internal.InitializerConfig.<>c__DisplayClass6.<TryGetInitializerFromEntityFrameworkSection>b__1(ContextElement e)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromEntityFrameworkSection(Type contextType)
at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
Then I created a class which inherits from DropAndSeedDbInitializer like so:
public class DummyInitializer : DropAndSeedDbInitializer<LicenceContext, TestSeedDataInitializer>
{
}
And I configured it in app.config like so:
<context type="Foo.LicenceContext, MyAssembly">
<databaseInitializer
type="Foo.DummyInitializer, MyAssembly" />
</context>
And then the DropAndSeedDbInitalizer ran and I could access my context without any problems.
So my question is: should I be able to configure this custom initializer in app.config? If I should, what am I doing wrong?
Thanks,
David

DbContext not initializing with SQL Server Compact in ASP.Net MVC

I have created a simple project using ASP.Net MVC template in Visual Studion 2013 Express for Web. It does not use any authentication. Then I installed EntityFramework (v6.0.1), EntityFramework.SqlServerCompact packages.
My DbContext class is very simple:
public class EditTestContext : DbContext
{
public EditTestContext() : base("EditTestContext")
{
}
public EditTestContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(
new DropCreateDatabaseIfModelChanges<EditTestContext>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new EditTestConfig());
}
}
The actual context object is created in the Unit of Work class:
public class EditTestUoW:IEditTestUoW,IDisposable
{
private DbContext dbContext;
public EditTestUoW()
{
CreateDbContext();
}
private void CreateDbContext()
{
dbContext = new EditTestContext();//NEW DBCONTEXT OBJECT IS CREATED
dbContext.Configuration.LazyLoadingEnabled = false;
dbContext.Configuration.ProxyCreationEnabled = false;
dbContext.Configuration.ValidateOnSaveEnabled = false;
}
public IRepository<EditTestModel> EditTestRepo
{
get
{
return new EFRepository<EditTestModel>(dbContext);
}
}
}
The connection string being used is:
<add name="EditTestContext" connectionString="Data Source=
|DataDirectory|EditTestDb.sdf;Max Database Size=256;
Max Buffer Size=1024;File Mode=Shared Read;
Persist Security Info=False;" providerName="System.Data.SqlServerCe.4.0" />
Now when I try to access any thing using this Context like:
var rep=UoW.EditTestRepo;
var list=rep.GetAll().ToList();
I am getting following exception on rep.GetAll() function:
System.InvalidOperationException: Sequence contains no matching element
On digging deeper, IQueryable from Repository class (DbSet<EditTest>) is throwing following exception:
The context cannot be used while the model is being created. This exception may
be thrown if the context is used inside the OnModelCreating method or if the same
context instance is accessed by multiple threads concurrently. Note that instance
members of DbContext and related classes are not guaranteed to be thread safe.
I thought it might have been caused by ninject, but it is still there even after I removed it.
What I am doing wrong here or something (some assembly reference etc.) is missing?
Well after some other search on the issue, I got this MSDN forum link. As was suggested by Rowan, I tried to manually initialize the context using following statement in my EFRepository class:
dbContext.Database.Initialize(false);
The application failed way before it was hitting the GetAll() method. But this exposed the stack trace which gave me some direction:
[InvalidOperationException: Sequence contains no matching element]
System.Linq.Enumerable.Single(IEnumerable`1 source, Func`2 predicate) +2614017
System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName
(DbProviderManifest providerManifest, String name) +146
.....Other Lines.....
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType
(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +138
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +38
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable
.get_Provider() +99
System.Linq.Queryable.Any(IQueryable`1 source) +50
Then searching for DbProviderManifestExtensions.GetStoreTypeFromName revealed that this is the line where EF was trying to get column type. I had specified UNIQUEIDENTIFIER for my Id column:
Property(x=> x.Id).HasColumnType("UNIQUEIDENTIFIER")
Once I commented this, all was well.
Though there is a request on Codeplex to provide the proper error message in case the column type is not valid for database provider.

"Signature of the body and declaration in a method implementation do not match"

UPDATE: I think I've eliminated Unity from the equation. See below for more details.
UPDATE 2: I think I may have eliminated Entity Framework fro the equation. See below for more details.
I have some code that is building a unity container, but it started failing with the above error message out of the blue. It works on other people's machines, but not mine. I deleted the folder the solution was in and refreshed everything from source control to ensure I had nothing that could be causing issues (e.g. duplicate assemblies lying around from a previous build).
Here is part of my code:
public static class UnityBootstrapper
{
public static IUnityContainer Initialise()
{
Trace.WriteLine("UnityBootstrapper.Initialise()");
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
Trace.WriteLine("UnityBootstrapper.BuildUnityContainer()");
var container = new UnityContainer();
// Other dependencies are registered here
// If the following line is commented out the container is build
// but, obviously, it won't resolve this dependency.
container.RegisterType<IUserAccessEntities, UserAccessEntities>(WebRequestLifetime);
// Other dependencies are registered here
return container;
}
The code apparently fails on the call to BuildUnityContainer(), and I can see that the trace statement I put inside that method is never displayed.
However, if I comment out the line that registers the UserAccessEntities class (which was code generated from Entity Framework 5) then the container is built. Naturally, when I ask for that dependency it can't resolve it, so the code just fails elsewhere.
I've Googled for solutions and they all seem to resolve around generics and moving the generic type from the method to the class level. I can't do that as EF5 creates the class and it puts generics on the properties. e.g
DbSet<MyTable> Tables { get; set; }
The only other thing I can think of is that I've extracted an interface from the EF5 generated class called IUserAccessEntities and the problem could lie there... but I used ReSharper to generate that, so it should be perfectly aligned.
UPDATE
Just to eliminate Unity from the equation, I tried to new up the UserAccessEntities on its own
private static void TestUae()
{
var uae = new UserAccessEntities(); //container.Resolve<IUserAccessEntities>();
Trace.WriteLine("Got the entities: " + uae);
}
And the call to TestUae() fails instead.
UPDATE 2
I created a new class, AlternativeEntities based on the interface I'd previously extracted. When I try to construct that directly it has a new exception: Method 'Set' in type 'AlternativeEntities' from assembly 'UserAccess.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
However, it does. There are two methods called set, both of which I've given a basic implementation:
public class AlternativeEntities : IUserAccessEntities
{
public DbSet<TEntity> Set<TEntity>() where TEntity : class
{
Trace.WriteLine("public DbSet<TEntity> Set<TEntity>() where TEntity : class");
return null;
}
public DbSet Set(Type entityType)
{
Trace.WriteLine("public DbSet Set(Type entityType)");
return null;
}
// Other methods and properties here.
}

Entity Framework: DbContext and setting the ProviderName

When you derive from DbContext and use the parameter-less constructor it will load a connection string from web.config. You also have the option of explicitly specifying the connectionString using one of the other DbContext constructors.
My particular situation dictates that the connection string CANNOT be specified in the web.config, as the location of the server/username and password are determined at runtime. Easy fix right? Just use the above mentioned constructor to specify the connection string? Wrong.
The problem is that when you specify the connection string using said constructor, it still attempts to use the default provider, so if you're using one or more non standard providers, as I am, it will not work.
I'm sure I can change the default provider in the web.config, but I want to use multiple providers so this will not do.
The only possible way around this that I can see is to use ObjectContext instead of DbContext, which seems to allow you to specify the provider along with the database connection string.
Is there any other way to do it? Is my workaround fairly reasonable?
I believe I can also create a DbContext from an ObjectContext instance.
Create your DbConnection manually and pass it to the DbContext constructor as follows:
var conn = DbProviderFactories.GetFactory("MY_CONN_PROVIDER").CreateConnection();
conn.ConnectionString = "MY_CONN_STR";
new DbContext(conn, true);
Notice the second parameter bool contextOwnsConnection is true. Since you don't re-use the connection elsewhere, it lets the context manage the connection and Dispose() it when needed.
You can get to the ObjectContext through IObjectContextAdapter:
((IObjectContextAdapter)context).ObjectContext
DbContext ("context" above) still wraps ObjectContext, so don't worry that you will have a new instance.
You can instantiate DbContext using this overload
public DbContext(ObjectContext objectContext, bool dbContextOwnsObjectContext) {}
for example:
public class YourDbContext : DbContext
{
public YourDbContext() : this(new YourObjectEntities(), dbContextOwnsObjectContext: true)
{}
}
Then you can set your connection string inside of YourObjectEntities:
public partial class YourObjectEntities : ObjectContext
{
public const string ConnectionString = "name=YourEntities"; // Get it from somewhere
public YourObjectEntities() : base(ConnectionString, "YourEntities")
{
// Some initialization, e.g. ContextOptions.LazyLoadingEnabled = false;
}
}
How you specify the provider there is your exercise.
Try like this ,
public DBDataContext _dataContex;
public DBDataContext DBContext
{
get
{
if (_dataContex== null)
{
_v= new DBDataContext(ConfigurationManager.ConnectionStrings["yourConnectinString"].ConnectionString);
}
return _dataContex;
}
}