【问题标题】:Issue when insert into mysql using PetaPoco Database.Insert<T>使用 PetaPoco Database.Insert<T> 插入 mysql 时出现问题
【发布时间】:2012-06-13 07:50:16
【问题描述】:

我有一个使用 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 字段为空。这种情况怎么样?

【问题讨论】:

  • 插入发生时产生的sql是什么?钩入Database 对象上的OnExecutedCommand 方法并打印出LastCommand 属性。

标签: mysql petapoco


【解决方案1】:

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

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

AutoIncrement 默认为 true。

【讨论】:

  • 非常感谢,它在上面的例子中有效,但是,在我的实际应用中,lowered_name 和 name 字段不是主键。它们是普通字段。我将编辑问题,以便您为我提供更多帮助。再次感谢!
  • 我正在考虑跳到 Peta-poco,但这个问题真的很棘手......关于解决方案的任何消息?
  • 我现在不确定是什么问题?你能在github.com/schotime/NPoco/issues上创建一个问题吗?谢谢。
猜你喜欢
  • 2011-08-27
  • 2020-11-30
  • 2010-10-16
  • 1970-01-01
  • 2020-11-10
  • 2021-07-01
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多