简体   繁体   English

如何扩展Entity Framework 6模型

[英]How to extend an Entity Framework 6 model

I am trying to extend an EF 6 code first model and I am getting an error. 我试图扩展EF 6代码的第一个模型,我收到一个错误。 I have searched and can't find much information on the issue. 我搜索过,找不到有关该问题的更多信息。 I have created a partial class of the same name as my model in the same namespace. 我在同一名称空间中创建了一个与我的模型同名的部分类。 Here is my model and the extension: 这是我的模型和扩展:

Model - 型号 -

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

Extension - 延期 -

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

The error I am getting is: 我得到的错误是:

{"The model backing the 'AiContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/?LinkId=238269 )."} {“自创建数据库以来,支持'AiContext'上下文的模型已更改。请考虑使用Code First Migrations更新数据库( http://go.microsoft.com/fwlink/?LinkId=238269 )。”}

I am getting this error when trying to call a stored procedure. 我试图调用存储过程时收到此错误。 More specifically, it is being thrown on this line: 更具体地说,它正在抛出这条线:

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

of this method: 这种方法:

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

I am familiar with the error. 我很熟悉这个错误。 EF obviously thinks I've made a change to the model and wants me to update the database. EF显然认为我已经对模型进行了更改,并希望我更新数据库。 Any ideas on why I am getting this error or another way to extend a model to add properties or business logic? 有关为什么我收到此错误或扩展模型以添加属性或业务逻辑的其他方法的任何想法? Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

If you use it only in the business logic and not intended to map it as a column in the database. 如果仅在业务逻辑中使用它,而不是将其映射为数据库中的列。 You need to add [NotMapped] attribute. 您需要添加[NotMapped]属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM