我正在运行EF 4.2 CF并想在我的POCO对象的某些列上创建索引.
举个例子,假设我们有这个员工类:
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我们经常通过EmployeeCode对员工进行搜索,因为有很多员工,因为性能原因而将索引编入索引会很不错.
我们能用流利的api以某种方式做到这一点吗?或者数据注释?
我知道可以执行这样的sql命令:
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ...");
Run Code Online (Sandbox Code Playgroud)
我非常想避免那样的原始SQL.
我知道这不存在,但寻找沿着这些方向的东西:
class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
internal EmployeeConfiguration()
{
this.HasIndex(e => e.EmployeeCode)
.HasIndex(e => e.FirstName)
.HasIndex(e => e.LastName);
}
}
Run Code Online (Sandbox Code Playgroud)
或者使用System.ComponentModel.DataAnnotationsPOCO可能看起来像这样(我知道这不存在):
public class Employee
{ …Run Code Online (Sandbox Code Playgroud) c# fluent-interface entity-framework-4 data-annotations ef-code-first
我正在寻找关于在MVC应用程序中使用Entity Framework Code-First时验证逻辑的"最佳"位置的建议,例如对实体的重复检查.
使用一个简单的例子:
public class JobRole
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
规则是"名称"字段必须是唯一的.
当我添加一个新的JobRole时,很容易在Job Role Repository中运行一个名称尚不存在的检查.
但是,如果用户编辑现有JobRole,并意外将名称设置为已存在的名称,我该如何检查?
问题是存储库中不需要"更新"方法,因为作业角色实体将自动检测更改,因此在尝试保存之前没有合理的位置来执行此检查.
到目前为止,我考虑了两个选项:
两者都不是真的很理想.使用ValidateEntry似乎相当晚(在保存之前)并且难以测试.使用服务可能会有人忘记从控制器调用它,让重复的数据通过.
有没有更好的办法?
.net validation asp.net-mvc design-patterns entity-framework
我有以下场景:
使用ef6的业务逻辑功能检查记录是否已存在.如果记录不存在,则将其插入数据库中.
像这样的东西
if (!product.Skus.Any(s => s.SkuCodeGroup == productInfo.SkuCodeGroup && s.SkuCode == productInfo.SkuCode))
AddProductSku();
Run Code Online (Sandbox Code Playgroud)
我有多个线程调用此业务逻辑函数.会发生什么:
如果使用相同的参数同时调用该函数,则两个实例都会检查该记录是否存在 - 并且它不存在.因此两个实例都插入相同的记录.
当在第一个实例上调用context.SaveChanges()时,一切正常.但是第二个SaveChanges()抛出异常,因为记录已经存在(数据库上有唯一索引).
如何实现这一点以避免异常?
我通过使用锁{}来解决它,但它创造了一个我不想要的瓶颈.
谢谢.
我想使用EF 5模型验证来避免数据库中的重复值,所以我使用这样的模型类:
[Table("MeasureUnits")]
public class MeasureUnit : IValidatableObject
{
public int MeasureUnitId { get; set; }
public string Symbol { get; set; }
public string Name { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
using (MeasureUnitRepository rep = new MeasureUnitRepository())
{
MeasureUnit measureUnit = rep.FindDuplicateBySymbol(this);
if (measureUnit != null)
yield return new ValidationResult(
"There is another unit with this symbol, you can't duplicate it",
new[] { "Symbol" });
}
}
Run Code Online (Sandbox Code Playgroud)
存储库类创建DbContext,实现IDisposable,具有查找副本的逻辑,它都可以正常工作.
但是,使用调试器我意识到每次插入或更新都会执行两次验证,因此存储库(和DbContext)也会被实例化并处理两次.
除此之外,还有另一个DbContext存在于控制器中但只是在模型类中找不到使用它的方法,除了在模型的构造函数中包含DbContext,但我觉得它不是正确的解决方案.
是否有更好的"正确"方式来实现此验证?
提前致谢.
我使用 C# 使用“从数据库生成”生成了数据库模型。POCO 类和上下文是使用 T4 模板生成的。一切工作正常,应用程序能够编辑、插入等,但我无法覆盖实体类中的 SaveChanges 方法。我需要这样做来添加业务逻辑。这是上下文类:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WebApplication1
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class IInvoiceEntities2 : DbContext
{
public IInvoiceEntities2 ()
: base("name=IInvoiceEntities2 ")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ …Run Code Online (Sandbox Code Playgroud)