簡體   English   中英

如何擴展Entity Framework 6模型

[英]How to extend an Entity Framework 6 model

我試圖擴展EF 6代碼的第一個模型,我收到一個錯誤。 我搜索過,找不到有關該問題的更多信息。 我在同一名稱空間中創建了一個與我的模型同名的部分類。 這是我的模型和擴展:

型號 -

namespace DataAccess.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Vehicle")]
    public partial class Vehicle
    {
        public Vehicle()
        {
            WorkOrders = new HashSet<WorkOrder>();
        }

        public int VehicleID { get; set; }

        public int CustomerID { get; set; }

        [StringLength(17)]
        public string VIN { get; set; }

        public int Mileage { get; set; }

        [StringLength(4)]
        public string Year { get; set; }

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

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

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

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

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

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

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

        public bool IsDeleted { get; set; }

        [StringLength(25)]
        public string License { get; set; }

        public bool? Is4WD { get; set; }

        [StringLength(25)]
        public string Color { get; set; }

        public string Notes { get; set; }

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

        public virtual Customer Customer { get; set; }

        public virtual ICollection<WorkOrder> WorkOrders { get; set; }
    }
}

延期 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.Models
{
    public partial class Vehicle
    {
        public string CustomerName { get; set; }
    }
}

我得到的錯誤是:

{“自創建數據庫以來,支持'AiContext'上下文的模型已更改。請考慮使用Code First Migrations更新數據庫( http://go.microsoft.com/fwlink/?LinkId=238269 )。”}

我試圖調用存儲過程時收到此錯誤。 更具體地說,它正在拋出這條線:

using (ObjectContext objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext)

這種方法:

public List<Vehicle> SearchVehicles(string vin,
                                            string license,
                                            string year,
                                            string make,
                                            string model)
        {
            SqlParameter vinParameter;
            if (vin != null)
            {
                vinParameter = new SqlParameter("vin", vin);
            }
            else
            {
                vinParameter = new SqlParameter("vin", "");
            }

            SqlParameter licenseParameter;
            if (license != null)
            {
                licenseParameter = new SqlParameter("license", license);
            }
            else
            {
                licenseParameter = new SqlParameter("license", "");
            }

            SqlParameter yearParameter;
            if (year != null)
            {
                yearParameter = new SqlParameter("year", year);
            }
            else
            {
                yearParameter = new SqlParameter("year", "");
            }

            SqlParameter makeParameter;
            if (make != null)
            {
                makeParameter = new SqlParameter("make", make);
            }
            else
            {
                makeParameter = new SqlParameter("make", "");
            }

            SqlParameter modelParameter;
            if (model != null)
            {
                modelParameter = new SqlParameter("model", model);
            }
            else
            {
                modelParameter = new SqlParameter("model", "");
            }

            using (AiContext dbContext = new AiContext())
            {
                using (ObjectContext objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext)
                {
                    return objectContext.ExecuteStoreQuery<Vehicle>("SearchVehicles  @VIN, " +
                                                                    "@License, @Year, " +
                                                                    "@Make, @Model",
                                                                    vinParameter,
                                                                    licenseParameter,
                                                                    yearParameter,
                                                                    makeParameter,
                                                                    modelParameter).ToList();
                }
            }
        }

我很熟悉這個錯誤。 EF顯然認為我已經對模型進行了更改,並希望我更新數據庫。 有關為什么我收到此錯誤或擴展模型以添加屬性或業務邏輯的其他方法的任何想法? 我在這里先向您的幫助表示感謝。

如果僅在業務邏輯中使用它,而不是將其映射為數據庫中的列。 您需要添加[NotMapped]屬性。

[NotMapped]
public string CustomerName { get; set; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM