3

我有一个使用 PetaPoco 重新实现 ASP.NET 的 Membership Provider、Role Provider 和 Profile Provider 的小项目。每个表都有两个共同的字段:name 和 lowered_name。当尝试使用 Database.Insert() 将新记录插入数据库时​​,我遇到了一个问题,无法插入 lowered_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);

但是,此代码不起作用,创建了一条新记录,但记录的 lowered_name 字段为空。当我将 db.Insert(cat) 更改为:

db.Execute("insert into category(name, lowered_name) values (@0, @1)", name, name.ToLower());

一切安好。

我为上面的这些提供程序完成了很多代码,它们与 PostGreSQL 和 SQL Server 配合得很好,我不想重新实现每个存储库的每个 Insert 函数(我在 AbstractRepository 类中创建了一个通用的 Insert 函数)。

你有什么主意吗?


如果 lowered_name 是主键,SchoTime 已经帮助我解决了这个问题。但是,在我的真实应用程序中,name 和 lowered_name 不是主键。

我使用一个抽象类:

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 和 lowered_name 字段为空。这种情况怎么办?

4

1 回答 1

5

因为您有一个主键lowered_name并且它似乎不是一个自动增量列,所以您必须将 autoIncrement 设置为 false ,如下所示。

[PrimaryKey("lowered_name", autoIncrement=false)]

AutoIncrement 默认为 true。

于 2012-06-10T08:45:57.560 回答