我正在尝试将数据绑定到表单,但我不断收到以下错误:
AbDesktop.FrmCreditCards.tmpData是一个'字段',但用作“类型”。
这是导致错误的代码,任何和所有帮助都将不胜感激。
public partial class FrmCreditCards : Form
{
tmpCreditCardData tmpData = new tmpCreditCardData() { CreditCardName = "Reece" };
private HotelConferenceEntities db = new HotelConferenceEntities();
private tblCreditCard databaseAccessObject;
private tmpData dataTransferObject;
public void databinding()
{
// Create forward map using AutoMapper - from database to dataTransfer
AutoMapper.Mapper.CreateMap<tblCreditCard, tmpData>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.CreditCardName));
// Create reverse map using AutoMapper - from dataTransfer to database
AutoMapper.Mapper.CreateMap<tmpData, tblCreditCard>()
.ForMember(dest => dest.CreditCardName, opt => opt.MapFrom(src => src.Name));
}
答案 0 :(得分:2)
CreateMap的泛型参数是个问题。
您必须使用类型tmpCreditCardData
而不是变量名tmpData
作为编译错误。
// Create forward map using AutoMapper - from database to dataTransfer
AutoMapper.Mapper.CreateMap<tblCreditCard, tmpCreditCardData>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.CreditCardName));
// Create reverse map using AutoMapper - from dataTransfer to database
AutoMapper.Mapper.CreateMap<tmpCreditCardData, tblCreditCard>()
.ForMember(dest => dest.CreditCardName, opt => opt.MapFrom(src => src.Name));
答案 1 :(得分:1)
您对类型和字段使用相同的名称 - 将字段tmpData重命名为其他内容 - 即:
tmpCreditCardData tmpData2 = new tmpCreditCardData
{ CreditCardName = "Reece" };
有一些关于约定的注释 - 约定指定类应以大写字母开头并命名为
MyFavouriteClass
和字段应该命名为
myFavouriteField
(以小写字母开头)
有人还将私人字段与下划线区分开来
private MyFavouriteClass _myFavouriteField
如果坚持使用它们,就不应该遇到这个问题。