我正在开发一个非常基本的NHibernate 3.2任务,将记录插入到现有的Postgres表中。我使用的是非常简单的对象,因此它们对这个问题有意义。
postgres表格如下:
CREATE TABLE cat
(
id serial NOT NULL,
"name" character varying(50) NOT NULL,
sex_id integer NOT NULL,
CONSTRAINT cat_pkey PRIMARY KEY (id),
CONSTRAINT cat_sex_id_fkey FOREIGN KEY (sex_id)
REFERENCES sex (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
CREATE TABLE sex
(
id serial NOT NULL,
"name" character varying(10) NOT NULL,
CONSTRAINT sex_pkey PRIMARY KEY (id),
CONSTRAINT sex_name_key UNIQUE (name)
)
WITH (
OIDS=FALSE
);
我的映射类如下:
public class CatMap : ClassMapping<Cat>
{
public CatMap()
{
Table("cat");
Id(x => x.Id, map =>
{
map.Column("id");
map.Generator(NHibernate.Mapping.ByCode.Generators.Native);
});
Property(x => x.Name, map =>
{
map.Column("name");
map.Length(50);
map.NotNullable(true);
});
ManyToOne(x => x.Sex, map =>
{
map.Column("Sex");
map.Unique(true);
map.ForeignKey("cat_sex_id_fkey");
});
}
}
public class SexMap : ClassMapping<Sex>
{
public SexMap()
{
Table("sex");
Id(x => x.Id, map =>
{
map.Column("id");
map.Generator(Generators.Native);
});
Property(x => x.Name, map =>
{
map.Column("name");
map.Unique(true);
map.Length(10);
map.NotNullable(true);
});
}
}
我的数据类如下:
public class Sex
{
public Sex()
{
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Cat
{
public Cat()
{
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Sex Sex { get; set; }
}
最后,包含我实际上试图使用以上所有内容的代码的类。
public class Class1
{
public string DoSomething()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
Postgres.Tables.Sex sex1 = new Postgres.Tables.Sex() { Name = "Male" };
Postgres.Tables.Sex sex2 = new Postgres.Tables.Sex() { Name = "Female" };
Postgres.Tables.Cat cat1 = new Postgres.Tables.Cat() { Name = "Cat1" };
cat1.Sex = sex1;
Postgres.Tables.Cat cat2 = new Postgres.Tables.Cat() { Name = "Cat2" };
cat2.Sex = sex2;
session.SaveOrUpdate(sex1);
session.SaveOrUpdate(sex2);
session.SaveOrUpdate(cat1);
session.SaveOrUpdate(cat2);
transaction.Commit();
}
}
return "I created the cats.";
}
private static ISessionFactory CreateSessionFactory()
{
NHibernate.Mapping.ByCode.ModelMapper modelMapper = new NHibernate.Mapping.ByCode.ModelMapper();
System.Type[] mappingTypes = typeof(Postgres.Tables.Mappings.CatMap).Assembly.GetExportedTypes().Where(t => t.Name.EndsWith("Map")).ToArray();
modelMapper.AddMappings(mappingTypes);
Configuration cfg = new Configuration();
cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>());
cfg.DataBaseIntegration(d =>
{
d.ConnectionString = "server=192.168.1.126;Port=5432;Database=simple;User Id=postgres;Password=postgres;";
d.Dialect<NHibernate.Dialect.PostgreSQL82Dialect>();
});
cfg.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());
return cfg.BuildSessionFactory();
}
}
我在“session.SaveOrUpdate(cat1)”收到GenericADOException并显示消息“无法插入:[DAL.Postgres.Tables.Cat] [SQL:INSERT INTO cat(name,Sex)VALUES(?,?) ;选择lastval()]“。 InnerException是“{”错误:42703:关系\“cat \”的列\“sex \”不存在“}”。
我有点难以理解如何将“sex1”正确地分配给“cat1”和“sex2”到“cat2”,以便第一个是男性,第二个是女性。
感谢您的任何意见。