简体   繁体   English

在Entity Framework Database First中更改代码后更新数据库

[英]Updating database after code changes in Entity framework Database First

I had a class named feedback generated via database first approach 我有一个通过数据库优先方法生成的名为反馈的类

public partial class Feedback
{
    public Nullable<byte> OverallExperienceFb { get; set; }
    public Nullable<byte> FunFb { get; set; }
    public Nullable<byte> FoodFb { get; set; }
    public Nullable<byte> TravelFb { get; set; }
    public Nullable<byte> PlanningFb { get; set; }
    public Nullable<byte> MusicFb { get; set; }
    public Nullable<byte> PlaceFb { get; set; }
    public Nullable<byte> WeatherFb { get; set; }
    public Nullable<int> Paid { get; set; }
    public int ID{get; set;}
}

I later added a constraint of range 0-5 on overallFb as following:- 后来我在overallFb上添加了范围为0-5的约束,如下所示:

public partial class TravellerTripStatus
{
    [Range(0,5)]
    public Nullable<byte> OverallExperienceFb { get; set; }
}

This change is reflecting on the GUI such that the edit form won't allow to enter value out of the range but how to make this change reflect into database (such that a check contraint automatically gets added to overallFB field of table definition)? 此更改反映在GUI上,以使编辑表单不允许输入超出范围的值,但是如何使此更改反映到数据库中以使检查overallFB自动添加到表定义的overallFB字段中)?

When i try to generate database from model, no such constraint is enforced. 当我尝试从模型生成数据库时,没有强制执行此类约束。

Adding constraints to a table is currently not possible with data annotations or fluid API. 使用数据注释或Fluid API当前无法向表添加约束。 But the workaround is to issue a SQL command in your Configuration.cs file inside your Seed method: 但是解决方法是在Seed方法内的Configuration.cs文件中发出SQL命令:

db.Database.ExecuteSqlCommand("ALTER TABLE TravellerTripStatus ADD CONSTRAINT CK_TravellerTripStatus_OverallExperienceFb CHECK (OverallExperienceFb BETWEEN 0 AND 5)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM