Dapper 无法将数据传递到数据库

Dapper cannot pass the data to the database

我遇到无法插入或更新数据库的问题。我正在使用 Dapper 并使用嵌套 class 作为我要传递数据的模型。

我有一个错误是:

The member Information of type TestProject.Models.Information cannot be used as a parameter value

我的模型如下:

public class Member
{
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
    [Required]
    public string Information MemberInfo { get; set; }
}

public class Information
{
    [Required]
    public string Email { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public string State { get; set; }
    [Required]
    public DateTime BirthDate { get; set; }
}

我的控制器如下:

public IHttpActionResult SetMemberInfo(Member models)
{
    using (TransactionScope trans = new TransactionScope())
    using (IDbConnection conn = new SqlConnection(GetConnection()))
    {
        conn.Open();

        int rowsAffected = conn.Execute("MemberInformation", models, commandType: CommandType.StoredProcedure);

        trans.Complete();

        return rowsAffected;
    }

    return Ok();
}

我的存储过程如下:

ALTER PROCEDURE dbo.[MemberInformation]
(
    @Username   NVARCHAR(100),
    @Password   NVARCHAR(100),
    @Email      NVARCHAR(100),
    @City       NVARCHAR(100),
    @State      NVARCHAR(100),
    @BirthDate  DATETIME
)
AS
BEGIN
 // TODO: Do something with the data
END

我将数据从客户端传递给控制器​​如下:

var models = {
    "Username": "MyUsername",
    "Password": "MyPassword",
    "MemberInfo": {
        "Email": "MyEmail",
        "City": "MyCity",
        "State": "MyState",
        "BirthDate": "2017-08-23"
    }
};

$.ajax({
                type: "POST",
                contentType: "application/json",
                url: "http://localhost/SetMemberInfo",
                data: JSON.stringify(models),
                dataType: "json",
                success: function (data) {
                    window.location.reload();
                },
                error: function (response) {
                    console.log(JSON.stringify(response));
                }
            });

但如果我没有将其放入嵌套 class(意味着信息 class 中的所有数据都移动到成员 class),一切正常。

如何告诉 dapper 将数据从嵌套的 class 中分离出来并将其分解为单个 class,即使在应用程序级别是嵌套的 class。

或者有其他解决方案吗?

非常感谢您的回答。

谢谢

嗯,我不认为 Dapper 对嵌套对象作为参数有这种支持。

您可以尝试的是:

  1. 完全按照存储过程的预期创建 DTO,并将 Member 对象映射到这个新类型。
public class MemberInformationParameter
{
    public MemberInformationParameter(Member member)
    {
            Username = member.Username;
            Password = member.Password;
            Email = member.MemberInfo.Email;
            City = member.MemberInfo.City;
            State = member.MemberInfo.State;
            BirthDate = member.MemberInfo.BirthDate;
    }

    public string Username { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public DateTime BirthDate { get; set; }
}
  1. 或者将参数作为动态对象传递,语法如下:

    conn.Execute("MemberInformation", new { Property1: value1, Property2: value2 }, commandType: CommandType.StoredProcedure);

[Table("studentclass")]
public partial class Studentclass
{
    public Studentclass()
    {
        Studentclasssyllabus = new HashSet<Studentclasssyllabus>();
    }

    public int Id { get; set; }
    public int Studentid { get; set; }
    public int Subjectclassid { get; set; }

    [Write(false)]
    [Computed]
    public virtual Student Student { get; set; }

    [Write(false)]
    [Computed]
    public virtual Subjectclass Subjectclass { get; set; }

    [Write(false)]
    [Computed]
    public virtual ICollection<Studentclasssyllabus> Studentclasssyllabus { get; set; 
}

[Table("schedule")]
public partial class Schedule
{
    public Schedule()
    {
        Attendance = new HashSet<Attendance>();
    }

    public int Id { get; set; }
    public int? Lecturerid { get; set; }
    public int? Subjectclassid { get; set; }
    public DateTime Date { get; set; }
    public TimeSpan Starttime { get; set; }
    public TimeSpan Endtime { get; set; }
    public int? Roomstatus { get; set; }
    public string Ownerjitsiid { get; set; }
    public string Generateroomname { get; set; }

    [Write(false)]
    [Computed]
    public virtual Lecturer Lecturer { get; set; }

    [Write(false)]
    [Computed]
    public virtual Subjectclass Subjectclass { get; set; }

    [Write(false)]
    [Computed]
    public virtual ICollection<Attendance> Attendance { get; set; }
}

}

当我将此标签“[Write(false)][Computed]”设置为模型时。当 dapper 更新实体时,它将传递该参数

  1. 添加 dapperextentions 包
  2. Dapper 扩展有一个名为 insert 或 update 的函数,可以接受像 Member 对象这样的模型。
  3. 自动地图();只要字段名称相同,就会映射所有其他属性。
public class MemberMapper : DapperExtensions.Mapper.ClassMapper<Member>
    {
        public MemberMapper()
        {
           Table("TableName if diffrent than the Model calss name");
           Map(f => f.MemberInfo).Ignore();
           AutoMap();
        }
    }