我正在尝试创建我的第一个 nhibernate 应用程序。当我运行程序命令窗口打开并创建数据库表。但是,将新行插入到 Team 表的事务失败,并出现错误“无效的演员表(检查您的映射以了解属性类型不匹配);” 行 session.Save(Team); 中的 program.cs 文件中发生错误。任何人都可以帮助找出为什么会发生这种情况?任何帮助,将不胜感激。这是我的代码
播放器.cs
namespace BDB.Entities
{
public class Player
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
public virtual string Birthdate { get; set; }
public virtual string Position { get; set; }
public virtual int Salary { get; set; }
public virtual Team Team { get; set; }
}
}
玩家地图.cs
namespace BDB.Mappings
{
public class PlayerMap : ClassMap<Player>
{
public PlayerMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Surname);
Map(x => x.Birthdate);
Map(x => x.Position);
Map(x => x.Salary);
References(x => x.Team);
}
}
}
团队.cs
namespace BDB.Entities
{
public class Team
{
public virtual int Id { get; protected set; }
public virtual string Country { get; set; }
public virtual string City { get; set; }
public virtual string Title { get; set; }
public virtual string Website { get; set; }
public virtual int Budget { get; set; }
public virtual List<Player> Players { get; set; }
public virtual List<Coach> Coaches { get; set; }
public Team()
{
Players = new List<Player>();
Coaches = new List<Coach>();
}
public virtual void AddPlayer(Player player)
{
player.Team = this;
Players.Add(player);
}
public virtual void AddCoach(Coach coach)
{
coach.Team = this;
Coaches.Add(coach);
}
}
}
TeamMap.cs
namespace BDB.Mappings
{
public class TeamMap : ClassMap<Team>
{
public TeamMap()
{
Id(x => x.Id);
Map(x => x.Country);
Map(x => x.City);
Map(x => x.Title);
Map(x => x.Budget);
Map(x => x.Website);
HasMany(x => x.Players)
.Inverse()
.Cascade.All();
HasMany(x => x.Coaches)
.Inverse()
.Cascade.All();
}
}
}
教练.cs
namespace BDB.Entities
{
public class Coach
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
public virtual int Experience { get; set; }
public virtual int Salary { get; set; }
public virtual Team Team { get; set; }
}
}
教练地图.cs
namespace BDB.Mappings
{
public class CoachMap : ClassMap<Coach>
{
public CoachMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Surname);
Map(x => x.Experience);
Map(x => x.Salary);
References(x => x.Team);
}
}
}
会话工厂.cs
namespace BDB
{
public class SessionFactory
{
public static ISessionFactory ConfigureSystem()
{
var connString = "server=.\\SQLEXPRESS;database=litest;integrated security=SSPI;";
var configuration = Fluently.Configure()
.Database(MsSqlConfiguration
.MsSql2008
.ConnectionString(connString)
.ShowSql
)
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<Team>()
.AddFromAssemblyOf<Player>()
.AddFromAssemblyOf<Coach>())
.BuildConfiguration();
var exporter = new SchemaExport(configuration);
exporter.Execute(true, true, false);
ISessionFactory sessionFactory = configuration.BuildSessionFactory();
return sessionFactory;
}
}
}
程序.cs
namespace BDB
{
class Program
{
static void Main(string[] args)
{
ISessionFactory sessionFactory = SessionFactory.ConfigureSystem();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
Team Team = new Team { Country = "lt", City = "kau", Title = "Zal", Budget = 10000, Website = "www.kz.lt" };
session.Save(Team);
transaction.Commit();
}
}
Console.ReadKey();
}
}
}