I am working on Fluent Nhibernate and getting the following error:
No persister for: System.Data.Entity.DynamicProxies.Customer_3FF392FD8867C34D66C0BEB65D29B9B13BAFD2651E0DD73A797E67ADE1455B21
Please tell me that how can I remove the proxy that are generated during mapping. Thanks In advance.
Just call:
session.GetSessionImplementation().PersistenceContext.Unproxy(entity)
Related
I'm setting up an ASP.NET MVC4 application and I'm using Ninject as my DI container.
In a previous project I used StructureMap with the following setup in my registry:
For<IAutoPersistenceConfiguration>()
.Use<Sql2008AutoPersistenceConfiguration>()
.Ctor<string>("connectionString")
.Is("SomeConnectionStringHere");
For<ISessionSource>()
.Singleton()
.Use(x => new SessionSource(x.GetInstance<IAutoPersistenceConfiguration>().Build()));
Now I try to a similar setup in Ninject. My try so far is:
Bind<IAutoPersistenceConfiguration>()
.To<Sql2008AutoPersistenceConfiguration>()
.WithConstructorArgument("connectionString", "SomeConnectionStringHere");
Bind<ISessionSource>()
.ToMethod(x => new SessionSource(
x.Kernel.Get<IAutoPersistenceConfiguration>().Build())).InSingletonScope();
When I run this I get an FluentConfigurationException:
An invalid or incomplete configuration was used while creating a SessionFactory.
The other classes are all the same; IAutoPersistenceConfiguration and Sql2008AutoPersistenceConfiguration are all the same as before. The version of NHibernate and FluentNHibernate are the same as before as well.
What am I doing incorrectly?
Turns out my Ninject setup was correct. The exception I got hid an inner exception that I did not look at initially (see my own comment to my question). Once I saw the inner exception all I had to do was fix my connection string and everything worked.
I am considering using Fluent NHibernate for my project and I haven't found any documentation on whether FH supports NHibernate settings such as show_sql and prepare_sql. I could live without show_sql in a pinch, but prepare_sql is important for ensuring good performance at run time.
Can anyone tell me if it's possible to configure these settings in Fluent NHibernate?
Yes, you can.
Fluently.Configure()
.Database(ConfigureDatabase())
.Mappings(ConfigureMapping)
.ExposeConfiguration(ModifyConfiguration)
.BuildConfiguration();
And now in ModifyConfiguration method you have plain NHibernate's Configuration object to modify
private void ModifyConfiguration(Configuration configuration)
{
// set parameters here like this:
configuration.Properties["show_sql"] = "true";
}
Some of the settings are exposed through the fluent API.
See here for examples: Database Configuration
Anything that isn't supported through specific fluent calls can be set by manipulating the native NHibernate.Cfg.Configuration object. Either way you can do everything in code that you can with the configuration file.
What namespace do I need to have in my project to execute the command;
ISession.Evict(obj)????
You should just need NHibernate and whatever you're using for proxy like NHibernate.ByteCode.Castle. Should have no problem with ISession with just those.
Note also that you'll need FluentNHibernate if you are using any of Fluent's items in the same class.
What is Castle proxy factory in NHibernate? What is its task? What does proxy mean in this case?
Castle can be used (amongst others, you have the choice, you can also use LinFu, Spring.NET, ...) to create dynamic proxies of your entities.
By default, NHibernate uses dynamic proxies to represent your entities; by doing so, it is able to return an object to you when you retrieve some entity from the DB, without all properties being populated. By using a dynamic proxy, it will only populate the entity once you really refer to a property.
(So it is some kind of lazy loading; not to be confused with lazy loading of collections / associations though).
This behaviour is the reason why NHibernate wants you to create every property as virtual by default: NHibernate will create a new class using this Castle (or LinFu, ...) proxy provider which inherits from your entity, and it will override all the properties so that it can 'inject' the code that is necessary to retrieve the necessary data from the DB.
You can disable this behaviour by specifying 'lazy=false' in your entity mapping. (Although, I do think that even if you disable this feature, NHibernate will still require that you use one of the proxy factories).
When you are selecting an entity from ISession you are getting not real entity instance - you are getting proxy object.
This proxy object inherits your entity and used by NHibernate to track changes made to the fields.
see it: http://en.wikipedia.org/wiki/Proxy_pattern
I am trying to get test running that uses FluentNHibernate to map my
entities.
I get unknown entity class error.
I have a Domain project that has my entities and the mappings in a seperate
folder in that project.
My test project has the Nhibrenate configuration in the App.Config.
Any ideas please???
Malcolm
EDIT: I have this method that creats the ISessionFactory but I dont think this code is working and this is why it does not recoginze me mappings exist at all. The SessionFacoryCreator is the assembly with my AppConfig and Recipe is my assembly where the Domain objects are. Please HELLLLLLP!!!!
public static ISessionFactory Create()
{
Configuration cfg = new Configuration()
.AddAssembly(typeof(SessionFactoryCreator).Assembly)
.AddAssembly(typeof(Recipe).Assembly);
return cfg.BuildSessionFactory();
}
Have you read the wiki? Your example shows nothing of hooking Fluent NHibernate into the session factory building, that'd be why it isn't working. Start with the Getting Started guide.
How do you add your Entities to the Hibernate framework? Are you using AnnotationConfiguration or just Configuration? If you are using annotations you must add your annotated entity to the framwork. Otherwise, if you are using xml hibernate mapping, the syntax could be worng (iirc)
NHibernate and I keep on getting the following error: MappingException: Unkown Entity Class
If you get MappingException error, there are two things you should check out:
Make sure the name of the *.hbm.xml is spelled correctly — not *.hmb.xml nor *.mbh.xml
Make sure the Build Action property is set to “Embeded Resource.”