使用db-migration确定数据库中字段的值的范围 [英] Determining the range of value for a field in database by using db-migration

查看:307
本文介绍了使用db-migration确定数据库中字段的值的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了Entity Framework 6.x,并且我已经通过代码优先的方式生成了我的数据库。
创建数据库后,我决定在我的数据库中进行一些更改。例如,我想确定我的模型中 Size 属性的值的范围。



我的模型:

  public class标签:Entity,ITag 
{
///< summary>
///大小可以是1,2,3或4
///< / summary>
[范围(1,4)]
public virtual int Size {get;组;

[必需]
[StringLength(25)]
public virtual string标题{get;组;

[StringLength(256)]
public virtual string描述{get;组; }

public virtual bool IsActive {get;组; }

public virtual ISet< ArticleTag> ArticleTags {get;组; }

public virtual ISet< ProjectTag> ProjectTags {get;组; }
}

迁移:

 命名空间Jahan.Blog.Web.Mvc.Migrations 
{
using System;
使用System.Data.Entity.Migrations;

public partial class Initial:DbMigration
{
public override void Up()
{
//我想编写一些这样的代码,可以提供愤怒的数据。 1到4:
//AlterColumn(\"dbo.Tag,Size,c => c.Int(nullable:false,defaultValue:1));
// ...但我不知道该怎么做
}

public override void Down()
{
}
}
}


解决方案

实体框架使用范围注释来指示如何在保存前验证数据, MVC生成客户端验证。如果您还希望在数据库中添加检查约束,您可以在Up迁移中执行此操作:

  public override void Up )
{
// Sql添加使用Sql Server语法的检查约束:
Sql(@ALTER Table dbo.Tags
ADD CONSTRAINT chk_Size
CHECK(Size IN(1,2,3,4)));
}


I've used Entity Framework 6.x and I've produced my database by code-first approach. After creating db, I've decided to some changes in my db. for example I want to determine a range of value for Size property in my model.

my model:

public class Tag : Entity, ITag
{
    /// <summary>
    /// Size can be 1, 2, 3 or 4
    /// </summary>
    [Range(1, 4)]
    public virtual int Size { get; set; }

    [Required]
    [StringLength(25)]
    public virtual string Title { get; set; }

    [StringLength(256)]
    public virtual string Description { get; set; }

    public virtual bool IsActive { get; set; }

    public virtual ISet<ArticleTag> ArticleTags { get; set; }

    public virtual ISet<ProjectTag> ProjectTags { get; set; }
}

Migration:

namespace Jahan.Blog.Web.Mvc.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class Initial : DbMigration
    {
       public override void Up()
       {
           // I want to write some code like this that can provide rage of data. 1 to 4:
           //AlterColumn("dbo.Tag", "Size", c => c.Int(nullable: false,defaultValue:1));
           //... but I don't know how can I do it.
       }

       public override void Down()
       {
       }
    }
}

解决方案

The Range annotation is used by the Entity Framework to indicate how to validate the data prior to saving, and by MVC to generate client side validation. If you also wish to add a check constraint to the database you can do it in your Up migration:

    public override void Up()
    {
        //Sql to add a check constraint using Sql Server syntax:
        Sql(@"ALTER Table dbo.Tags 
        ADD CONSTRAINT chk_Size 
        CHECK (Size IN (1, 2, 3, 4))");
    }

这篇关于使用db-migration确定数据库中字段的值的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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