ASP.NET MVC 4,EF5,模型中的独特属性 - 最佳实践? [英] ASP.NET MVC 4, EF5, Unique property in model - best practice?

查看:91
本文介绍了ASP.NET MVC 4,EF5,模型中的独特属性 - 最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC 4,EF5,代码优先,SQL Server 2012 Express



最佳做法是强制执行唯一值一个模型?我有一个地方课程的url属性应该是每个地方都是唯一的。

  public class放置
{
[ScaffoldColumn(false)]
public virtual int PlaceID {get;组; }

[DisplayName(Date Added)]
public virtual DateTime DateAdded {get;组; }

[必需(ErrorMessage =需要地名)
[StringLength(100)]
public virtual string Name {get;组; }

public virtual string URL {get;组; }
};

为什么不能只有一个[Unique]数据注释? >

我已经看过1或2个讨论,但没有谈到最佳实践。使用代码首先你可以以某种方式告诉数据库在数据库中的字段上设置一个唯一的约束?



什么是最简单的方法 - 什么是最佳实践?

解决方案

令人兴奋的是,现在最好的做法是使用内置验证而是使用 FluentValidation 。然后,代码将非常容易阅读和超级维护,因为验证将以单独的类别进行管理,意味着较少的意大利面条代码。



您尝试的伪代码

  [Validator(typeof(PlaceValidator))] 
class Place
{
public int Id {get;组; }
public DateTime DateAdded {get;组; }
public string Name {get;组; }
public string Url {get;组; }
}

public class PlaceValidator:AbstractValidator< Place>
{
public PlaceValidator()
{
RuleFor(x => x.Name).NotEmpty()。WithMessage(Place Name is required)。 ,100);
RuleFor(x => x.Url).Must(BeUniqueUrl).WithMessage(Url already exists);
}

private bool BeUniqueUrl(string url)
{
返回新的DataContext()。Places.FirstOrDefault(x => x.Url == url) == null
}
}


ASP.NET MVC 4, EF5, Code First, SQL Server 2012 Express

What is best practice to enforce a unique value in a model? I have a places class that has a 'url' property that should be unique for every place.

public class Place
{
      [ScaffoldColumn(false)]
      public virtual int PlaceID { get; set; }

      [DisplayName("Date Added")]
      public virtual DateTime DateAdded { get; set; }

      [Required(ErrorMessage = "Place Name is required")]
      [StringLength(100)]
      public virtual string Name { get; set; }

      public virtual string URL { get; set; }
};

Why isn't there just a [Unique] data annotation you can place on it?

I have seen 1 or 2 discussions on this, but no talk of best practice. Using Code First can you somehow tell the database to set a unique constraint on the field in the database?

What is easiest way - and what is best practice?

解决方案

As crazy as it might sound the best practice nowadays is to not use built-in validation and instead use FluentValidation. Then the code will be very easy to read and super-maintainable since validation will be managed on separate class meaning less spaghetti code.

Pseudo-example of what you are trying to achieve.

[Validator(typeof(PlaceValidator))]
class Place
{
    public int Id { get; set; }
    public DateTime DateAdded { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
}

public class PlaceValidator : AbstractValidator<Place>
{
    public PlaceValidator()
    {
        RuleFor(x => x.Name).NotEmpty().WithMessage("Place Name is required").Length(0, 100);
        RuleFor(x => x.Url).Must(BeUniqueUrl).WithMessage("Url already exists");
    }

    private bool BeUniqueUrl(string url)
    {
        return new DataContext().Places.FirstOrDefault(x => x.Url == url) == null
    }
}

这篇关于ASP.NET MVC 4,EF5,模型中的独特属性 - 最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆