我有以下课程:
public class Publication
{
public int Id { get; set; }
public string Title { get; set; }
public string Headline { get; set; }
public DateTime Published { get; set; }
public ProductContact Contact { get; set; }
}
public class ProductContact
{
public string FullName { get; set; }
public string JobTitle { get; set; }
public string Email { get; set; }
}
与此结构关联的表“Publications”具有所有这些字段(包括 ProductContact 的属性)。
如果我尝试插入发布行(包含 ProductContact 信息),程序会抛出异常:
System.NotSupportedException: The member Contact of type ProductContact cannot be used as a parameter value
因此,我添加了一个映射器来将 ProductContact 属性映射到 Properties 表中的字段:
public PublicationMapper ()
{
TableName = "Publications";
Map(x => x.Contact.FullName).Column("ContactFullName");
Map(x => x.Contact.JobTitle).Column("ContactJobTitle");
Map(x => x.Contact.Email).Column("ContactEmail");
AutoMap();
}
使用这个映射器,我得到了同样的例外。
然后,我为 Contact 字段添加了忽略语句,告诉 Dapper 不要在插入语句中包含此元素
Map(x => x.Contact).Ignore();
在这种情况下,我得到另一个例外:
System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@FullName".
说明 Dapper 完全忽略了这个属性,上一步添加的映射没有效果。
有没有办法将 ProductContact 属性映射到表字段?
谢谢你。