如何扩展实体框架6模型
本文关键字:实体 框架 模型 扩展 何扩展 | 更新日期: 2023-09-27 18:21:49
我正在尝试扩展一个EF6代码优先的模型,但遇到了一个错误。我已经搜索过了,找不到太多关于这个问题的信息。我在同一个命名空间中创建了一个与我的模型同名的分部类。这是我的型号和扩展名:
型号-
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; }
}
}
我得到的错误是:
{"自数据库已创建。考虑使用代码优先迁移进行更新数据库(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; }