[英]Using Auto Increment with MySQL, NHibernate and C#
EDIT - It turns our the problem was completely unrelated to the primary key mapping - the errors were pointing me in the wrong direction in this particular instance. 编辑-原来我们的问题与主键映射完全无关-在这种特定情况下,错误将我指向错误的方向。
I'm trying to have a simple auto_incremented primary key map to a property in c#. 我正在尝试将简单的auto_incremented主键映射到c#中的属性。 Here's my hbm mapping I have at the moment:
这是我目前的hbm映射:
<class name="Lobby" table="lobby">
<id name="Id" type="int" unsaved-value="null" >
<column name="id" not-null="true"/>
<generator class="identity"/>
</id>
...
</class>
And here's my class: 这是我的课:
public class Lobby
{
public virtual int Id { get; set; }
...
}
The table is declared as follows: 该表声明如下:
CREATE TABLE IF NOT EXISTS `lobby`
(
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
...
)
When I try to create an object using the repository: 当我尝试使用存储库创建对象时:
using (var session = mSessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Save(newEntry);
transaction.Commit();
}
I get a PropertyTypeException: 我得到一个PropertyTypeException:
Exception occurred getter of Lobby.Id> Object does not match target type.
Lobby.Id>对象的吸气剂发生异常,与目标类型不匹配。
I've tried changing the property to a long and a short and this hasn't helped. 我尝试将属性更改为多头和空头,但这并没有帮助。 I've also tried making the column not
auto_incremented
and using the "increment" generator, but this generates an insert query that doesn't include the 'id' property, which then fails because the Primary Key needs to be set. 我也试图使不列
auto_incremented
并使用“增量”发生器,但产生一个插入查询不包括“ID”属性,然后失败,因为主键需要设置。
Can anyone see what I am doing wrong and are there any examples of how to do this properly? 谁能看到我在做什么错,并且有任何示例可以正确地做到这一点吗?
int
cannot be null. int
不能为null。 You can try unsaved-value="0"
. 您可以尝试
unsaved-value="0"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.