EF提供者特定的模型配置(如果为true,则为必需)

时间:2019-07-02 06:29:20

标签: .net entity-framework-core ef-core-2.0

仅在标记为true时如何设置为必填?

例如:

Property(t => t.DataQualityReview)
.If(t.IsDataQualityReview == true)
.ThenIsRequired()

1 个答案:

答案 0 :(得分:0)

我认为数据库将无法实施这种约束。 您可以通过几种方法以不同的方式执行需求。

选项1 (首选): 如果您可以更改数据库的架构,则可以创建一个单独的实体/表,该实体/表将表示def update_population(self,win_rate,population_size): models = [] json_file = open('model_num.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) for counter in range(population_size): loaded_model.load_weights('network' + str(counter) + '.h5') models.append(loaded_model) top_3_models = sorted(zip(win_rate, models), reverse=True)[:3] top_models = [] for item in top_3_models: top_models.append(item[1]) new_models = [] clone_counter = 3 for model in top_models: new_models.append(model) old_weights = [] for layer in model.layers: old_weights.append(layer.get_weights()) for i in range(clone_counter): new_model = clone_model(model) new_weights = copy.copy(old_weights) for weight in new_weights: weight += normal(0,.1) new_model.set_weights(new_weights) new_models.append(new_model) if clone_counter ==3: clone_counter = 2 counter = 0 for model in new_models: model.save('network' + str(counter) + '.h5') counter +=1

DataQualityReview

这里的想法是public class Foo { // optional DataQualityReview public int? DataQualityReviewId { get; set; } public DataQualityReview DataQualityReview { get; set; } } public class DataQualityReview { public int DataQualityReviewId { get; set; } // required properties of a review public string Comments { get; set; } public int Rating { get; set; } } 可能有Foo,也可能没有。如果确实有评论,则可以强制执行其他必需的属性(例如DataQualityReviewComments等)。

选项2 :如果无法更改数据库。您可以在类本身上强制执行此操作。

Rating

如图所示,您无法在public class Foo { // notice `private set` public bool IsDataQualityReview { get; private set; } public string DataQualityReview { get; private set; } public void UpdateReview(string review) { DataQualityReview = review; IsDataQualityReview = review != null; } } 之外更改IsDataQualityReviewDataQualityReview。然后,您公开一个方法Foo,该方法会根据评论更改这两个属性。