c# - 如何扩展 Entity Framework 6 模型

标签 c# wpf entity-framework-6

我正在尝试扩展 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; }
    }
}

我得到的错误是:

{"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)."}

我在尝试调用存储过程时遇到此错误。更具体地说,它被扔在这条线上:

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

关于c# - 如何扩展 Entity Framework 6 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824857/

相关文章:

c# - 我们可以使用枚举作为类型安全的实体 ID 吗?

c# - VB.NET 和 C# 中的面向对象编程

c# - 字典在后台加载

c# - 在 ASP.NET MVC2 中将 http 204 "no content"返回给客户端

c# - 将 subview 中的元素绑定(bind)到父 View 模型的属性

c# - Caliburn.Micro MVVM 框架中的对象绑定(bind)与 SelectedItem

entity-framework - 在单一模型中结合代码优先和数据库优先?

c# - 从属角色具有多个具有不同值的主体

wpf - 如何从类库项目 (dll) 中的公共(public)静态方法显示 WPF 窗口

c# - Entity Framework 代码首先使用导航属性一个Key