我正在尝试用Fluent NHibernate映射我现有的数据库我得到错误:
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'."}
我是Fluent的新手,我不知道如何修复它? (也许是因为id是字符串?)
这是我的模特课:
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 = "adamm@wp.pl",
_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");
}
}
}
我只有一个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");
}
}
}
这是创建我的表的脚本(它已经存在):
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]
答案 0 :(得分:1)
而不是依赖于自动化:
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 ISessionFactory BuildSessionFactory()
{
return Fluently
.Configure()
.Database(
MsSqlConfiguration
.MsSql2005
.ConnectionString(c => c.FromConnectionStringWithKey("gwd"))
)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>())
.ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
.BuildSessionFactory();
}
此外,您可能需要将User
属性设为虚拟。
这是一个使用SQLite的完整工作示例,我用它来说明示例配置:
public class User
{
public virtual string IdUser { get; set; }
public virtual string Name { get; set; }
public virtual string Email { get; set; }
public virtual string Password { get; set; }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
Id(x => x.IdUser);
Map(x => x.Email);
Map(x => x.Name);
Map(x => x.Password);
}
}
class Program
{
static void Main(string[] args)
{
if (File.Exists("data.db3"))
{
File.Delete("data.db3");
}
using (var factory = CreateSessionFactory())
{
using (var connection = factory.OpenSession().Connection)
{
ExecuteQuery("create table Users(IdUser string primary key, Name string, Email string, Password string)", connection);
}
using (var session = factory.OpenSession())
using (var tx = session.BeginTransaction())
{
session.Save(new User()
{
IdUser = "adapol",
Name = "Adam Mickiewicz",
Email = "adamm@wp.pl",
});
tx.Commit();
}
}
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard.UsingFile("data.db3").ShowSql()
)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<UserMap>()
)
.BuildSessionFactory();
}
static void ExecuteQuery(string sql, IDbConnection connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = sql;
command.ExecuteNonQuery();
}
}
}
答案 1 :(得分:0)
我的猜测是你正在尝试使用AutoMapping,但是你将自动映射器指向映射命名空间而不是你的模型。这导致FNH尝试自动化您的实际映射类。 您可以将AutoMap指向模型命名空间,但查看代码可能会遇到更多困难。 在这个阶段,使用普通的非自动映射器可能会更好。
答案 2 :(得分:0)
我猜你正在运行一个过时的Fluent NHibernate版本;如果你运行1.1,你会得到一个更有帮助的例外。当您的班级没有映射身份证时,您(以前)有90%的时间会获得该异常。
我很确定automapper没有找到你的Id属性。首先,你得到了那个例外;第二,您的ClassMap
显示您正在使用名为IdUser
的属性作为该实体中的ID;第三,默认情况下配置automapper以仅搜索名为Id
的属性。
我建议您阅读automapping wiki page,因为它涵盖了所有这些内容。