In my quest to further my knowledge, I'm trying to get get NHibernate running.
I have the following structure to my solution
Core Class Library Project
Infrastructure Class Library Project
MVC Application Project
Test Project
In my Core project I have created the following entity:
using System;
namespace Core.Domain.Model
{
public class Category
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
In my Infrastructure Project I have the following mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Core.Domain.Model"
assembly="Core">
<class name="Category" table="Categories" dynamic-update="true">
<cache usage="read-write"/>
<id name="Id" column="Id" type="Guid">
<generator class="guid"/>
</id>
<property name="Name" length="100"/>
</class>
</hibernate-mapping>
With the following config file:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">server=xxxx;database=xxxx;Integrated Security=true;</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="cache.use_query_cache">false</property>
<property name="adonet.batch_size">100</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Infrastructure" />
</session-factory>
</hibernate-configuration>
In my test project, I have the following Test
[TestMethod]
[DeploymentItem("hibernate.cfg.xml")]
public void CanCreateCategory()
{
IRepository<Category> repo = new CategoryRepository();
Category category = new Category();
category.Name = "ASP.NET";
repo.Save(category);
}
I get the following error when I try to run the test:
Test method Volunteer.Tests.CategoryTests.CanCreateCategory threw exception: NHibernate.MappingException: No persister for: Core.Domain.Model.Category.
Any help would be greatly appreciated. I do have the cfg build action set to embedded resource.
Thanks!
The Build Action of the XML mapping file must be set to Embedded Resource in the Infrastructure assembly. When you use the following instruction in your config file: <mapping assembly="Infrastructure" /> it will look for mappings as embedded resources in this assembly.
I am guessing the problem is that your configuring your SessionFactory in your Test assembly without telling it that your mappings are in your Core assembly, something like
ISessionFactory factory = new Configuration().Configure()
.AddAssembly(typeof(Category).Assembly) <========***
.BuildSessionFactory();
If that doesn't help, post your configuration code.
Cheers,
Berryl
Related
I am having my database and other storage on azure cloud storage.i am using Entity Code first appoach but problem is when i am trying to read connection string from cloud storage using CloudConfigurationManager.GetSetting() i am getting below error:
Error:The argument 'nameOrConnectionString' cannot be null, empty or contain only white space.
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code
This is my Azure project:Demo.Web.Azure
It has 2 configurations file:
1)ServiceConfiguration.Cloud.cscfg:
<Role name="Demo.Web">
<Setting name="MyConnectionString"
value="Server=----;Database=DemoEntity;User ID=---;Password=---&w;Trusted_Connection=False;
Encrypt=True;MultipleActiveResultSets=True;" />
<Setting name="Demo.Storage"
value="DefaultEndpointsProtocol=https;AccountName=---;AccountKey=------"
/>
2)ServiceConfiguration.Local.cscfg:
<Role name="Demo.Worker">
<Setting name="Demo.Storage"
value="DefaultEndpointsProtocol=https;AccountName=---;AccountKey=------"
/>
3)ServiceDefinition.csdef:
<ServiceDefinition name="Demo.Web.Azure" schemaVersion="2014-06.2.4">
<WebRole name="RepuGuard.Web" vmsize="Small">
<ConfigurationSettings>
<Setting name="MyConnectionString" />
<Setting name="Demo.Storage" />
</ConfigurationSettings>
</WebRole>
<WorkerRole name="Demo.Worker" vmsize="Small">
<ConfigurationSettings>
<Setting name="MyConnectionString" />
<Setting name="Demo.Storage" />
</ConfigurationSettings>
</WorkerRole>
</ServiceDefinition>
This is my Context file which read Database connection string:
public partial class MyDemoDBContext : DbContext, IDisposable
{
public ObjectContext Context { get; set; }
public MyDemoDBContext()
: base(CloudConfigurationManager.GetSetting("MyConnectionString"))//Getting Error here
{
Context = ((IObjectContextAdapter)this).ObjectContext;
}
public MyDemoDBContext (string connectionString)
: base(connectionString)
{
Context = ((IObjectContextAdapter)this).ObjectContext;
}
}
Getting error here:
public MyDemoDBContext()
: base(CloudConfigurationManager.GetSetting("MyConnectionString"))
{
Context = ((IObjectContextAdapter)this).ObjectContext;
}
If you have multiple projects under solution, try setting up the startup project will resolve this issue.(sry for the delay)
A spelling error in appsettings.json caused the same System.ArgumentException for me. I discovered the string "ConnectionStrings" had turned into "CondfSnectionStrings" I dunno why. My eye just glossed over the problem until another programmer pointed it out to me.
I am struggling with NHibernate. I have a little ASP MVC website. I managed to setup NHibernate but when I want to retrieve data I get this error:
Additional information: could not initialize a collection: [Lore.Models.DatabaseModels.Statue.Keywords#1]
[SQL: SELECT keywords0_.IdStatueKeyword as IdStatue1_1_, keywords0_.IdKeyword as IdKeyword1_, keyword1_.IdKeyword as IdKeyword4_0_, keyword1_.Name as Name4_0_,
keyword1_.Description as Descript8_4_0_
FROM StatueKeyword keywords0_ left outer join Statue keyword1_ on keywords0_.IdKeyword=keyword1_.IdKeyword
WHERE keywords0_.IdStatueKeyword=?]
Also I am not sure if I implemented the many-to-many relationship right. This is my table structure:
Statue
IdStatue
Name
Keyword
IdKeyword
Name
StatueKeyword
IdStatueKeyword
IdStatue
IdKeyword
Statue Class:
public class Statue
{
public Statue()
{
Keywords = new List<Keyword>();
}
public int IdStatue { get; set; }
public string Name { get; set; }
public IList<Keyword> Keywords { get; set; }
}
Keyword Class
public class Keyword
{
public int IdKeyword { get; set; }
public string Name { get; set; }
}
Statue hbm file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Statue" table="Statue" lazy="false" >
<id name="IdStatue" column="IdStatue">
<generator class="identity" />
</id>
<property name="Name" column="Name" not-null="true" type="System.String" />
<bag name="Keywords" table="StatueKeyword" lazy="false">
<key column="IdStatueKeyword"/>
<many-to-many class="Keyword" column="IdKeyword"/>
</bag>
</class>
</hibernate-mapping>
Keyword hbm file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Keyword" table="Statue" lazy="false" >
<id name="IdKeyword" column="IdKeyword">
<generator class="identity" />
</id>
<property name="Name" column="Name" not-null="true" type="System.String" />
<property name="Description" column="Description" not-null="true" type="System.String" />
</class>
</hibernate-mapping>
I need to make this website for a master thesis so any help is much appreciated!
The issue should/could be in the <key> mapping:
<bag name="Keywords" table="StatueKeyword" lazy="false">
<!-- <key> is representing column where current Statue ID should be searched
while the below one seems to be the ID column of the pairing table
so instead of this
<key column="IdStatueKeyword"/>
use this: -->
<key column="IdStatue"/>
<many-to-many class="Keyword" column="IdKeyword"/>
</bag>
Also check these:
6.3. Collections of Values and Many-To-Many Associations
23.2. Author/Work - documented example with many-to-many
Small cite:
The foreign key from the collection table to the table of the owning class is declared using a <key> element.
Another tip, if you do have an ID column of the pairing table, you should try to use augmented feature:
6.7. Using an <idbag>
Another cite from doc about idbag:
Note that the update performance of an <idbag> is much better than a regular <bag>! NHibernate can locate individual rows efficiently and update or delete them individually, just like a list, map or set.
Finally, I (personally) would descourage you from using many-to-many. My view is, that it is better to avoid it. See: 24. Best Practices (a cite:)
Don't use exotic association mappings.
Good usecases for a real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, we think that most associations are one-to-many and many-to-one, you should be careful when using any other association style and ask yourself if it is really neccessary.
Maybe later also check these:
Am I doing many to many incorrectly when using fluent nhibernate?
How to create NHibernate HasManyToMany relation
many-to-many with extra columns nhibernate
Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?
My log4net configuration is this,
<?xml version="1.0" encoding="utf-8" ?>
<log4netConfiguration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="log4net.Config" value="log4net.config" />
</appSettings>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\my_logs/my_web_logs/my_log_%date{ddMMyyyy}.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="30MB" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
<!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />-->
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</log4netConfiguration>
I have a Logger helper class as,
public static class Logger
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static log4net.ILog Log
{
get { return log; }
}
}
In my assembly info, I have this entry,
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
log4net.config is the config file added to the web project.
In the code I log using the logger class,
Logger.Log.Info("User visits Sign In Page.");
Logging has been working when I set up the above setting. But suddenly logging has stopped working.
But when I created a new asp.net mvc website with above settings, logging works for that.
I tried with IIS Express and Local IIS. In both cases logging works for the test application I have created.
I cannot figure out why it's not logging? How can I diagnose this? What are the possible issues?
Solved by myself, reason was "for some reason" log4net configuration was not loaded from assembly info. Still I do not know why that happens.
I tried so many fixes proposed by different posts. Finally fixed the issue.
Solution mentioned in this post helped me to solve the issue.
I have added following configuration,
<!--These settings load the log4net configuration.-->
<add key="log4net.Config" value="log4net.config"/>
<add key="log4net.Config.Watch" value="True"/>
It starts logging!
Then I removed following line from assembly info,
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
The reason is that Log4Net tries to load the config from the assembly that first uses LogManager.GetLogger(). If it's one of your class libraries it will simply ignore the attribute in all other assemblies.
The easiest way to fix it is to invoke LogManager in your start file (like Program.cs or Global.asax):
var logger = LogManager.GetLogger(typeof(Program));
logger.Info("Application started.");
//rest of app init.
Doing that will get you the expected behavior with the assembly attribute.
I found log4net won't load the webconfig configuration unless you call log4net.Config.XmlConfigurator.Configure(); on start up.
protected void Application_Start()
{
// your other codes
log4net.Config.XmlConfigurator.Configure(); // must have this line
Logger = log4net.LogManager.GetLogger(typeof(MvcApplication));
}
I had a similar issue: I would not get any logging output, when I ran my assembly from IIS or IISExpress.
However, none of the answers above worked for me.
In my case the solution was to specify the path to the config file as an absolute path. It turned out that IISExpress does not set the current directory to the bin folder and log4net would not find the config file, so I had to use this workaround:
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
// remove file:// part from uri
UriBuilder ub = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(ub.Path);
var fi = new FileInfo(Path.Combine(Path.GetDirectoryName(path), "Logging.config.xml"));
if (fi.Exists)
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);
var logger = log4net.LogManager.GetLogger(typeof(WebApiApplication));
logger.Info("Application started.");
}
else
{
throw new FileNotFoundException("log4net config file not found", fi.FullName);
}
I have the following Java class defined in src/java
package org.davisworld.trip;
public class AirportHbm {
private long id;
private String name;
private String iata;
private String state;
private String lat;
private String lng;
// getters/setters defined
}
I have the hbm.cfg.xml file defined as follows in conf/hibernate:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping resource="AirportHbm.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I have the AirportHbm.hbm.xml file configured as follows in conf/hibernate:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.davisworld.trip.AirportHbm" table="usgs_airports">
<id name="id" column="airport_id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="airport_name" not-null="true" />
</property>
<property name="iata" type="java.lang.String">
<column name="locid" not-null="true" />
</property>
<property name="state" />
<property name="lat" column="latitude" />
<property name="lng" column="longitude" />
</class>
</hibernate-mapping>
And lastly, I have an AirportHbmConstraints.groovy file in the src/java folder:
package org.davisworld.trip
class AirportHbmConstraints {
static constraints = {
name()
iata(maxSize:3)
state(maxSize:2)
lat()
lng()
}
}
When I try to run the app, I get this error when Spring is initializing the web app context:
Caused by: java.lang.ClassCastException: org.davisworld.trip.AirportHbmConstraints cannot be cast to groovy.lang.Script
The tutorial I was following originally said that the AirportHbmConstraints.groovy file shouldn't have a class; it should just be a script:
package org.davisworld.trip
static constraints = {
name()
iata(maxSize:3)
state(maxSize:2)
lat()
lng()
}
But when I do that, STS gives me a compiler error:
Groovy:Modifier 'static' not allowed here.
Anyone know what I'm doing wrong? What is the correct way to apply constraints in Groovy to a Java domain class?
Many thanks,
Vito
When using constraints scripts with a Hibernate domain you do not use a class declaration or the static modifier as explained in section 15.3 Adding Constraints of the Grails documentation.
The correct constraint script would be:
constraints = {
iata maxSize: 3
state maxSize: 2
}
Note that fields without a constraint and the parentheses on the field declaration are optional.
I'm starting a new project for work, and I decided I want to give MVC a shot. It's a small internal site for a commute challenge.
I want to use Spring.NET for Validation. I have used Spring.NET before in Web Forms, but with no code behind as in traditional ASP.NET, how do I use the Page Validation framework Spring.NET provides?
Edit 1:
In an attempt to try this myself, here is what I have:
Web.Config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
</sectionGroup>
</configSections>
<appSettings>
<add key="RouteValidator" value="RouteValidator"/>
<add key="UserValidator" value="UserValidator"/>
</appSettings>
<spring>
<context>
<resource uri="config://spring/objects"/>
<resource uri="~/Config/Spring.Web.cfg.xml" />
<resource uri="~/Config/Spring.Validation.cfg.xml" />
</context>
<parsers>
<parser type="Spring.Validation.Config.ValidationNamespaceParser, Spring.Core" />
</parsers>
</spring>
<system.web>
<httpModules>
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web" />
</httpModules>
</system.web>
</configuration>
Spring.Web.Cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<description>
Foo MVC Controller declarations.
</description>
<object id="HomeController" type="Foo.MVC.Web.Controllers.HomeController, Foo.MVC.Web"></object>
<object id="AccountController" type="Foo.MVC.Web.Controllers.RouteController, Foo.MVC.Web"></object>
<object id="RouteController" type="Foo.MVC.Web.Controllers.RouteController, Foo.MVC.Web"></object>
<object id="Spring.Web.UI.Controls.ValidationError" abstract="true">
<property name="Renderer">
<object type="Spring.Web.UI.Validation.IconValidationErrorsRenderer, Spring.Web">
<property name="IconSrc" value="validation-error.gif"/>
</object>
</property>
</object>
<object id="Spring.Web.UI.Controls.ValidationSummary" abstract="true">
<property name="Renderer">
<object type="Spring.Web.UI.Validation.DivValidationErrorsRenderer, Spring.Web">
<property name="CssClass" value="validationError"/>
</object>
</property>
</object>
<object id="standardPage" abstract="true">
<property name="MasterPageFile" value="~/Views/Shared/Site.master"/>
<property name="CssRoot" value="~/Content/"/>
<property name="ImagesRoot" value="~/Content"/>
</object>
</objects>
My validation file is very standard and basically a copy and paste from another project, therefore I didn't include it.
Now the problem I have is how do I use it? How do I get application context? My web forms project users Spring.Web.UI.Page, but I'm worried because the default pages in MVC derive from System.Web.Mvc.ViewPage, so that isn't going to work.
Or am I just not able to use Spring.NET's framework for MVC quite yet?
Thanks!
Thanks for any assistance.
You definitely can use Spring with ASP.Net MVC. You need to register that you are using it in the Global.ascx class then the framework will create Controllers based on what you have defined in your config file.
public class MvcApplication : System.Web.HttpApplication
{
...Routes stuff...
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));
RegisterRoutes(RouteTable.Routes);
}
}
public class ControllerFactory : IControllerFactory
{
public IController CreateController(RequestContext requestContext, string controllerName)
{
return IoC.Resolve<IController>(controllerName);
}
public void ReleaseController(IController controller)
{
//This is a sample implementation
//If pooling is used write code to return the object to pool
if (controller is IDisposable)
{
(controller as IDisposable).Dispose();
}
controller = null;
}
}
public static class IoC
{
static readonly IObjectFactory Factory
= new XmlObjectFactory(new FileSystemResource
(HttpContext.Current.Server.MapPath("~/Config/Spring.config")));
public static T Resolve<T>(string name)
{
return (T)Factory.GetObject(name);
}
}
Just make sure that the path to your spring config file is correct! This was adapted from this link.
On a wider note, this approach does not allow you to Spring the page classes, and being an MVC architecture, where views are pretty dumb classes, does not really support rich validation in the view itself in the manner you suggest. Look at either including the validation in the Model (post-back) in JQuery.
In best of my knowledge, the Spring Validation is not supported up to the latest release of ASP.NET MVC (1.0) and Spring.NET framework (1.3).
As far as incorporating Spring.NET with MVC, you can use MvcContrib project and come to the same code-base as posted by Colin Desmond (but you don't have to do the dirty work yourself).