我正在尝试创建我的第一个nhibernate应用程序。当我运行程序命令窗口打开并创建数据库表。但是,向Team表插入新行的事务失败,并显示错误“Invalid Cast(检查映射是否存在属性类型不匹配);”在session.Save(Team);中的program.cs文件中发生错误。任何人都可以帮助找出为什么会这样?任何帮助,将不胜感激。这是我的代码
Player.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; }
}
}
PlayerMap.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);
}
}
}
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();
}
}
}
Coach.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; }
}
}
CoachMap.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);
}
}
}
SessionFactory.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;
}
}
}
Program.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();
}
}
}
答案 0 :(得分:2)
乍一看有一件事是不正确的是玩家和教练属性的声明:
public virtual List<Player> Players { get; set; }
public virtual List<Coach> Coaches { get; set; }
问题是他们都使用具体类型List<>
。 NHibernate在管理您的实体时使用它自己的集合类型,因此它必须能够使用除原始列表之外的那些(因为它使用具有更改跟踪事物的集合等)。将声明更改为:
public virtual ICollection<Player> Players { get; set; }
public virtual ICollection<Coach> Coaches { get; set; }
请注意,这些接口仍然接受您的List<>
,但也可以包含私有NHibernate自己的集合。