如果使用单个实体,使用automapper对我来说很好

时间:2015-02-16 22:48:40

标签: javascript jquery asp.net-mvc entity-framework c#-5.0

如果我要添加两个或更多实体,我必须在单个类中声明所有属性,以便从Ajax调用中的View中正确地将它们映射到JS。

只是检查我是否遗漏了AutoMapper可以做的事情,我没有使用......

例如,如果只使用一个实体(Customer),则AutoMapper中的以下内容可以正常工作:

Mapper.CreateMap<CustomerDTO, Customer>();
                    var customer = Mapper.Map<CustomerDTO, Customer>(custDTO);

如果使用两个或更多实体,我必须执行以下操作:

using (var dbcxtTrans = DbContext.Database.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
{
    Customer cust = new Customer();
    cust.Address1 = custProjDTO.customer_Address1;
    cust.Address2 = custProjDTO.customer_Address2;
    cust.CellPhone = custProjDTO.customer_CellPhone;
    cust.City = custProjDTO.customer_City;
    cust.Company = custProjDTO.customer_Company;
    cust.CreatedDate = custProjDTO.customer_CreatedDate;
    cust.Email = custProjDTO.customer_Email;
    cust.FirstName = custProjDTO.customer_FirstName;
    cust.HomePhone = custProjDTO.customer_HomePhone;
    cust.IMAddress = custProjDTO.customer_IMAddress;
    cust.LastName = custProjDTO.customer_LastName;
    cust.State = custProjDTO.customer_State;
    cust.UpdatedDate = custProjDTO.customer_UpdatedDate;
    cust.UserName = custProjDTO.customer_UserName;
    cust.Website = custProjDTO.customer_Website;
    cust.Zip = custProjDTO.customer_Zip;

    DbContext.Customers.Add(cust);
    await DbContext.SaveChangesAsync();

    Project proj = new Project();
    proj.CustomerID = cust.CustomerID;
    proj.CategoryID = custProjDTO.project_CategoryID;
    proj.CreatedDate = custProjDTO.project_CreatedDate;
    proj.Description = custProjDTO.project_Description;
    proj.Name = custProjDTO.project_Name;
    proj.Notes = custProjDTO.project_Notes;
    proj.PriorityID = custProjDTO.project_PriorityID;
    proj.Quote = custProjDTO.project_Quote;
    proj.StatusID = custProjDTO.project_StatusID;
    proj.UpdatedDate = custProjDTO.project_UpdatedDate;

    DbContext.Projects.Add(proj);
    await DbContext.SaveChangesAsync();

    dbcxtTrans.Commit();
}

这是因为我的属性名称必须与视图中的名称一致(见下文)

@model YeagerTechDB.ViewModels.Customers.CustomerProject

@{
    ViewBag.Title = "Create Customer & Project";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

<div class="body-content">
    <h4>Customer/Project</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <fieldset>
        <legend>Customer</legend>
        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                @Html.EditorFor(model => model.customer_UserName, new { htmlAttributes = new { @class = "form-control", @placeholder = "UserName" } })
                @Html.ValidationMessageFor(model => model.customer_UserName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-md-11 col-sm-11 col-xs-11">
                @Html.EditorFor(model => model.customer_Email, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email" } })
                @Html.ValidationMessageFor(model => model.customer_Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                @Html.EditorFor(model => model.customer_Company, new { htmlAttributes = new { @class = "form-control", @placeholder = "Company" } })
                @Html.ValidationMessageFor(model => model.customer_Company, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_FirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "First Name" } })
                @Html.ValidationMessageFor(model => model.customer_FirstName, "", new { @class = "text-danger" })
            </div>
            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_LastName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Last Name" } })
                @Html.ValidationMessageFor(model => model.customer_LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                @Html.EditorFor(model => model.customer_Address1, new { htmlAttributes = new { @class = "form-control", @placeholder = "Address1" } })
                @Html.ValidationMessageFor(model => model.customer_Address1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                @Html.EditorFor(model => model.customer_Address2, new { htmlAttributes = new { @class = "form-control", @placeholder = "Address2" } })
                @Html.ValidationMessageFor(model => model.customer_Address2, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_City, new { htmlAttributes = new { @class = "form-control", @placeholder = "City" } })
                @Html.ValidationMessageFor(model => model.customer_City, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-2 col-sm-2 col-xs-2">
                @Html.EditorFor(model => model.customer_State, new { htmlAttributes = new { @class = "form-control", @placeholder = "State" } })
                @Html.ValidationMessageFor(model => model.customer_State, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-3 col-sm-3 col-xs-3">
                @Html.EditorFor(model => model.customer_Zip, new { htmlAttributes = new { @class = "form-control", @placeholder = "Zip" } })
                @Html.ValidationMessageFor(model => model.customer_Zip, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_HomePhone, new { htmlAttributes = new { @class = "form-control", @placeholder = "Home Phone" } })
                @Html.ValidationMessageFor(model => model.customer_HomePhone, "", new { @class = "text-danger" })
            </div>
            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_CellPhone, new { htmlAttributes = new { @class = "form-control", @placeholder = "Cell Phone" } })
                @Html.ValidationMessageFor(model => model.customer_CellPhone, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_Website, new { htmlAttributes = new { @class = "form-control", @placeholder = "Website" } })
                @Html.ValidationMessageFor(model => model.customer_Website, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @Html.EditorFor(model => model.customer_IMAddress, new { htmlAttributes = new { @class = "form-control", @placeholder = "IM Address" } })
                @Html.ValidationMessageFor(model => model.customer_IMAddress, "", new { @class = "text-danger" })
            </div>
        </div>
    </fieldset>

    <fieldset>
        <legend>Project</legend>
        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-4 col-md-4 col-sm-4 col-xs-4">
                @Html.EditorFor(model => model.project_Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "ProjectName" } })
                @Html.ValidationMessageFor(model => model.project_Name, "", new { @class = "text-danger" })
            </div>
            <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">
                @Html.EditorFor(model => model.project_Description, new { htmlAttributes = new { @class = "form-control", @placeholder = "Description" } })
                @Html.ValidationMessageFor(model => model.project_Description, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-3 col-md-3 col-sm-3 col-xs-3">
                @Html.DropDownListFor(model => model.project_CategoryID, new SelectList(ViewBag.Categories, "CategoryID", "CategoryDescription"), "-- Select Category --", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.project_CategoryID, "", new { @class = "text-danger" })
            </div>
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                @Html.DropDownListFor(model => model.project_PriorityID, new SelectList(ViewBag.Priorities, "PriorityID", "PriorityDescription"), "-- Select Priority --", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.project_PriorityID, "", new { @class = "text-danger" })
            </div>
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                @Html.DropDownListFor(model => model.project_StatusID, new SelectList(ViewBag.Statuses, "StatusID", "StatusDescription"), "-- Select Status --", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.project_StatusID, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-10 col-md-10 col-sm-10 col-xs-10">
                @Html.EditorFor(model => model.project_Quote, new { htmlAttributes = new { @class = "form-control", @placeholder = "Quote" } })
                @Html.ValidationMessageFor(model => model.project_Quote, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-10 col-md-10 col-sm-10 col-xs-10">
                @Html.EditorFor(model => model.project_Notes, new { htmlAttributes = new { @class = "form-control", @placeholder = "Notes" } })
                @Html.ValidationMessageFor(model => model.project_Notes, "", new { @class = "text-danger" })
            </div>
        </div>
    </fieldset>
    <div class="form-group">
        <div>
            <button type="submit" id="btnCustomerProjectCreate" class="btn btn-default col-offset-2"><span class="glyphicon glyphicon-save"></span>Create</button>
        </div>
    </div>
</div>

<div>
    @Html.ActionLink("Back to List", "Index")
    @Html.Hidden("customerprojectCreateUrl", Url.Action("Create", "CustomerProjects", new { area = "Customers" }))
</div>

@section Scripts {
    <script>
        $(document).ready(function ()
        {
            if (typeof contentCreateCustomerProject == "function")
                contentCreateCustomerProject();
        });
    </script>
}

反过来,必须与我的JS属性对齐:

var customerProject_Input = {
        customer_UserName: $('#customer_UserName').val(),
        customer_Email: $('#customer_Email').val(),
        customer_Company: $('#customer_Company').val(),
        customer_FirstName: $('#customer_FirstName').val(),
        customer_LastName: $('#customer_LastName').val(),
        customer_Address1: $('#customer_Address1').val(),
        customer_Address2: $('#customer_Address2').val(),
        customer_City: $('#customer_City').val(),
        customer_State: $('#customer_State').val(),
        customer_Zip: $('#customer_Zip').val(),
        customer_HomePhone: $('#customer_HomePhone').val(),
        customer_CellPhone: $('#customer_CellPhone').val(),
        customer_Website: $('#customer_Website').val(),
        customer_IMAddress: $('#customer_IMAddress').val(),
        customer_CreatedDate: currdate,
        customer_UpdatedDate: null,
        project_Name: $('#project_Name').val(),
        project_Description: $('#project_Description').val(),
        project_CategoryID: $('#project_CategoryID').val(),
        project_PriorityID: $('#project_PriorityID').val(),
        project_StatusID: $('#project_StatusID').val(),
        project_Quote: $('#project_Quote').val(),
        project_Notes: $('#project_Notes').val(),
        project_CreatedDate: currdate,
        project_UpdatedDate: null
    };

反过来,只能在一个类中定义(见下文):

using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace YeagerTechDB.ViewModels.Customers
{
    [Serializable, DataContract(IsReference = true)]
    public class CustomerProject
    {
        //Customer    
        [ScaffoldColumn(true)]
        [Display(Name = "ID")]
        [DataMember]
        public short customer_CustomerID { get; set; }

        [Required]
        [StringLength(256)]
        [DataMember]
        public string customer_UserName { get; set; }

        [Required]
        [StringLength(50)]
        [EmailAddress]
        [DataMember]
        public string customer_Email { get; set; }

        [StringLength(50)]
        [DataMember]
        public string customer_Company { get; set; }

        [StringLength(50)]
        [DataMember]
        public string customer_FirstName { get; set; }

        [StringLength(50)]
        [DataMember]
        public string customer_LastName { get; set; }

        [StringLength(50)]
        [DataMember]
        public string customer_Address1 { get; set; }

        [StringLength(50)]
        [DataMember]
        public string customer_Address2 { get; set; }

        [StringLength(50)]
        [DataMember]
        public string customer_City { get; set; }

        [StringLength(2)]
        [DataMember]
        public string customer_State { get; set; }

        [StringLength(10)]
        [DataType(DataType.PostalCode)]
        [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Must match 99999 or 99999-9999 format")]
        [DataMember]
        public string customer_Zip { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Must match 999-999-9999 format")]
        [DataMember]
        public string customer_HomePhone { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Must match 999-999-9999 format")]
        [DataMember]
        public string customer_CellPhone { get; set; }

        [StringLength(100)]
        [DataType(DataType.Url)]
        [DataMember]
        public string customer_Website { get; set; }

        [StringLength(50)]
        [DataType(DataType.Url)]
        [DataMember]
        public string customer_IMAddress { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Created")]
        [DataMember]
        public DateTime customer_CreatedDate { get; set; }

        [Display(Name = "Updated")]
        [DataType(DataType.DateTime)]
        [DataMember]
        public DateTime? customer_UpdatedDate { get; set; }


        //Project

        [ScaffoldColumn(false)]
        [Editable(false)]
        [Display(Name = "Proj ID")]
        [DataMember]
        public short project_ProjectID { get; set; }

        [ScaffoldColumn(false)]
        [Editable(true)]
        [Display(Name = "Cust ID")]
        [DataMember]
        public short project_CustomerID { get; set; }

        [Required(ErrorMessage = "Required!")]
        [StringLength(30)]
        [Display(Name = "Project Name")]
        [DataMember]
        public string project_Name { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        [DataMember]
        public string project_Description { get; set; }

        [ScaffoldColumn(true)]
        [Display(Name = "Category")]
        [DataMember]
        public short project_CategoryID { get; set; }

        [ScaffoldColumn(true)]
        [Display(Name = "Priority")]
        [DataMember]
        public short project_PriorityID { get; set; }

        [ScaffoldColumn(true)]
        [Display(Name = "Status")]
        [DataMember]
        public short project_StatusID { get; set; }

        [DataType(DataType.Currency)]
        [DataMember]
        public decimal? project_Quote { get; set; }

        [DataType(DataType.MultilineText)]
        [DataMember]
        public string project_Notes { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Created")]
        [DataMember]
        public DateTime project_CreatedDate { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Updated")]
        [DataMember]
        public DateTime? project_UpdatedDate { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

两个类中的属性名称不同,请尝试创建详细的映射配置,如:

Mapper.CreateMap<Customer, CustomerDTO>()
    .ForMember(dest => dest.Address1, 
        opts => opts.MapFrom(src => src.customer_Address1))
    .ForMember(dest => dest.Address2, 
        opts => opts.MapFrom(src => src.customer_Address2))
    .ForMember(dest => dest.CellPhone, 
        opts => opts.MapFrom(src => src.customer_CellPhone))
    .ForMember(dest => dest.City, 
        opts => opts.MapFrom(src => src.customer_City))
    .ForMember(dest => dest.Company, 
        opts => opts.MapFrom(src => src.customer_Company))
    .ForMember(dest => dest.CreatedDate, 
        opts => opts.MapFrom(src => src.customer_CreatedDate))
    .ForMember(dest => dest.Email, 
        opts => opts.MapFrom(src => src.customer_Email))
    .ForMember(dest => dest.FirstName, 
        opts => opts.MapFrom(src => src.customer_FirstName))
    .ForMember(dest => dest.HomePhone, 
        opts => opts.MapFrom(src => src.customer_HomePhone))
    .ForMember(dest => dest.IMAddress, 
        opts => opts.MapFrom(src => src.customer_IMAddress))
    .ForMember(dest => dest.LastName, 
        opts => opts.MapFrom(src => src.customer_LastName))
    .ForMember(dest => dest.State, 
        opts => opts.MapFrom(src => src.customer_State))
    .ForMember(dest => dest.UpdatedDate, 
        opts => opts.MapFrom(src => src.customer_UpdatedDate))
    .ForMember(dest => dest.UserName, 
        opts => opts.MapFrom(src => src.customer_UserName))
    .ForMember(dest => dest.Website, 
        opts => opts.MapFrom(src => src.customer_Website))
    .ForMember(dest => dest.Zip, 
        opts => opts.MapFrom(src => src.customer_Zip));

另外,对于保存对象,您不需要两次调用:

await DbContext.SaveChangesAsync();

只做一次:

 DbContext.Projects.Add(proj);
 DbContext.Customers.Add(cust);
 await DbContext.SaveChangesAsync();