0

我正在尝试将数据绑定到表单,但我不断收到以下错误:

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));

        }
4

2 回答 2

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));
于 2013-07-23T11:19:49.483 回答
1

您对类型和字段使用相同的名称 - 将字段 tmpData 重命名为其他名称 - 即:

    tmpCreditCardData tmpData2 = new tmpCreditCardData
      { CreditCardName = "Reece" };

有一些关于约定的评论 - 约定指定类应以大写字母开头并命名为

MyFavouriteClass 

和字段应该命名为

myFavouriteField 

(以小写字母开头)

也有人用下划线区分私有字段

private MyFavouriteClass _myFavouriteField

如果你坚持他们,你不应该遇到这个问题。

于 2013-07-23T11:18:37.910 回答