我有一个小项目,使用PetaPoco重新实现ASP.NET的成员资格提供程序,角色提供程序和配置文件提供程序。每个表都有两个常用字段:name和lower_name。当我尝试使用Database.Insert()将新记录插入数据库时,我遇到了一个问题,lower_name字段无法插入。
例如,我们有一个名为Category
的表CREATE TABLE `category` (
`name` varchar(100) NOT NULL,
`lowered_name` varchar(100) NOT NULL,
PRIMARY KEY (`lowered_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
分类实体:
[TableName("category"), PrimaryKey("lowered_name")]
public class Category
{
[Column("name")]
public string Name { get; set; }
[Column("lowered_name")]
public string LoweredName { get; set; }
}
我使用PetaPoco插入一个新类别,如下所示
var name = Guid.NewGuid()。ToString(); var cat = new Category(){Name = name,LoweredName = name.ToLower()};
var db = new Database("Test");
db.Insert(cat);
var retrievecat = db.SingleOrDefault<Category>("where lowered_name like @0", name.ToLower());
Console.WriteLine(retrievecat.LoweredName);
Assert.IsTrue(retrievecat != null);
但是,此代码不起作用,创建了新记录但记录的reduced_name字段为空。 当我将db.Insert(cat)更改为:
时db.Execute("insert into category(name, lowered_name) values (@0, @1)", name, name.ToLower());
一切都很好。
我为上面的这些提供程序完成了很多代码,它们与PostGreSQL和SQL Server配合使用,我不想重新实现每个存储库的每个Insert函数(我在AbstractRepository中创建了一个公共的Insert函数)类)。
你有什么想法吗?
我使用抽象类:
public abstract class AbstractEntity
{
#region Fields
private string _name;
#endregion Fields
#region Properties
[PetaPoco.Column(Name = "id")]
public long Id { get; set; }
[Required]
[PetaPoco.Column(Name = "name")]
public string Name
{
get { return _name; }
set
{
_name = value;
LoweredName = value.ToLower();
}
}
[Required]
[PetaPoco.Column(Name = "lowered_name")]
public string LoweredName { get; set; }
[PetaPoco.Column(Name = "description")]
public string Description { get; set; }
#endregion Properties
#region Initialization
public AbstractEntity()
{
}
public AbstractEntity(string name)
{
Name = name;
}
#endregion Initialization
}
用户实体:
[PetaPoco.TableName("vietspring_security_user"), PetaPoco.PrimaryKey("id")]
public class User:AbstractEntity
{
#region Private Variables
private string _email;
#endregion
#region Constructors
public User():base()
{
InitMembers();
}
public User(int id)
{
Id = id;
InitMembers();
}
public User(string name): base(name)
{
Name = name;
InitMembers();
}
#endregion
#region Properties
[PetaPoco.Column("password")]
public virtual string Password { get; set; }
[PetaPoco.Column("password_format")]
public virtual int PasswordFormat { get; set; }
[PetaPoco.Column("password_salt")]
public virtual string PasswordSalt { get; set; }
[PetaPoco.Column("email")]
public virtual string Email
{
get
{
return _email;
}
set
{
_email = value;
LoweredEmail = value.ToLower();
}
}
[PetaPoco.Column("lowered_email")]
public virtual string LoweredEmail { get; set; }
[PetaPoco.Column("password_question")]
public virtual string PasswordQuestion { get; set; }
[PetaPoco.Column("password_answer")]
public virtual string PasswordAnswer { get; set; }
[PetaPoco.Column("comments")]
public virtual string Comments { get; set; }
[PetaPoco.Column("is_approved")]
public virtual bool IsApproved { get; set; }
[PetaPoco.Column("is_locked_out")]
public virtual bool IsLockedOut { get; set; }
[PetaPoco.Column("creation_date")]
public virtual DateTime CreationDate { get; set; }
[PetaPoco.Column("last_activity_date")]
public virtual DateTime LastActivityDate { get; set; }
[PetaPoco.Column("last_login_date")]
public virtual DateTime LastLoginDate { get; set; }
[PetaPoco.Column("last_locked_out_date")]
public virtual DateTime LastLockedOutDate { get; set; }
[PetaPoco.Column("last_password_change_date")]
public virtual DateTime LastPasswordChangeDate { get; set; }
[PetaPoco.Column("failed_password_attempt_count")]
public virtual int FailedPasswordAttemptCount { get; set; }
[PetaPoco.Column("failed_password_attempt_window_start")]
public virtual DateTime FailedPasswordAttemptWindowStart { get; set; }
[PetaPoco.Column("failed_password_answer_attempt_count")]
public virtual int FailedPasswordAnswerAttemptCount { get; set; }
[PetaPoco.Column("failed_password_answer_attempt_window_start")]
public virtual DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }
[PetaPoco.ResultColumn]
public virtual IList<Application> Applications { get; set; }
#endregion
...
}
在MembershipProvider中,我将以下方法称为创建新用户:
var userId = Convert.ToInt64(_userRepository.Insert(user));
_userRepository.AddApplication(app.Id, userId);
status = MembershipCreateStatus.Success;
我在保存用户方面取得了成功,但name和lowest_name字段为空。这种情况怎么样?
答案 0 :(得分:5)
因为您的主键为lowered_name
并且它似乎不是自动增量列,所以您必须将autoIncrement设置为false,如下所示。
[PrimaryKey("lowered_name", autoIncrement=false)]
默认情况下,AutoIncrement为true。