I'm trying to map myexisting database by Fluent NHibernate I get error:
The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'many-to-one' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'."}
I'm newbie in Fluent and I don't know how to fix it ? (maybe it is because id is string ?)
Here is my model class:
namespace Server.Model
{
public partial class User
{
string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
string _email;
public string Email
{
get { return _email; }
set { _email = value; }
}
TypeOfUser _typeOfUser;
public TypeOfUser TypeOfUser
{
get { return _typeOfUser; }
set { _typeOfUser = value; }
}
string _idUser;
public string IdUser
{
get { return _idUser; }
set { _idUser = value; }
}
public string Password { get; set; }
public static void AddUserTest()
{
var sessionFactory = BuildSessionFactory();
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(new User()
{
_idUser = "adapol",
_name = "Adam Mickiewicz",
_email = "[email protected]",
_typeOfUser = Model.TypeOfUser.NormalUser
});
}
}
}
private static ISessionFactory BuildSessionFactory()
{
AutoPersistenceModel model = CreateMappings();
return Fluently.Configure().Database(MsSqlConfiguration.MsSql2005
.ConnectionString(c => c.FromConnectionStringWithKey("gwd"))).Mappings(m => m.AutoMappings.Add(model))
.ExposeConfiguration((Configuration config) => new SchemaExport(config).Create(false, true)).BuildSessionFactory();
}
private static AutoPersistenceModel CreateMappings()
{
return AutoMap
.Assembly(System.Reflection.Assembly.GetCallingAssembly())
.Where(t => t.Namespace == "Server.Mappings");
}
}
}
I have only one classMap
namespace Server.Mappings
{
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
Id(x => x.IdUser,"IdUser");
Map(x => x.Email);
Map(x => x.Name);
Map(x => x.Password);
Map(x => x.TypeOfUser,"Type");
}
}
}
This is script to create my table( it already exists):
USE [GWD]
GO
/****** Object: Table [dbo].[Users] Script Date: 09/02/2010 23:08:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
[IDuser] [nvarchar](50) NOT NULL,
[Type] [int] NOT NULL,
[Name] [nvarchar](max) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[IDuser] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]