简体   繁体   English

在哪里放置函数和验证代码实体框架

[英]Where to put functions and validation code Entity framework

Where and how (code example would be great) can I remove business logic from auto generated class for example Customer.cs under my Model.edmx 在哪里以及如何(代码示例会很棒)我可以从自动生成的类中删除业务逻辑,例如我的Model.edmx下的Customer.cs

When I edit something in the designer and then save that changes EF update class so I need to enter for example default values (I know I can set them in designer) again. 当我在设计器中编辑某些内容然后保存更改EF更新类时,我需要输入例如默认值(我知道我可以在设计器中设置它们)。

public Customer()
{
    this.Blocked = false;
    this.Code = "#00000";
    this.Contacts = new ObservableListSource<Contact>();
}

Also how to create/where to put some basic validation (this.Code cannot be empty string or null) ? 另外如何创建/在哪里放置一些基本的验证(this.Code不能为空字符串或null)?

Thanks. 谢谢。

Classes generated by Entity Framework are marked with partial keyword and you can extend them adding new file and creating other part of the partial class. 实体框架生成的类用partial关键字标记,您可以扩展它们添加新文件并创建部分类的其他部分。 This also prevent your changes from being overwritten when you update the model. 这还可以防止在更新模型时覆盖您的更改。

And maybe what even more important, EF adds set of partial methods to every mapped property, which allows you to add your own validation logic: 也许更重要的是,EF为每个映射属性添加了一些部分方法,允许您添加自己的验证逻辑:

On Property Changing - include code to execute before the change occurs, such as property validation. On Property Changing - 包括在更改发生之前执行的代码,例如属性验证。 The value parameter is the value to which the property is changing. value参数是属性更改的值。 Implement this method to validate a property change before it occurs. 实现此方法以在属性更改发生之前对其进行验证。 To prevent the change from being made, you must throw an exception. 要防止更改,您必须抛出异常。

On Property Changed - include code to execute after the change occurs, such as logging the change. On Property Changed -包括在更改后的代码来执行,如记录的变化。

from How to: Execute Business Logic During Scalar Property Changes 来自如何:在标量属性更改期间执行业务逻辑

Your partial class should look like that: 你的部分课应该是这样的:

public partial class Customer()
{
    partial void OnCodeChanging(string value) 
    {
        if(string.IsNullOrEmpty(value))
            throw new InvalidOperationException ("value cannot be null or empty");
    }

}

You have to make sure it exists in the same namespace as the other part, created by EF. 您必须确保它与EF创建的其他部分位于同一名称空间中。

Check out this project I was working on... https://github.com/NickStrupat/EntityFrameworkCodeFirstTriggers 看看我正在研究的这个项目... https://github.com/NickStrupat/EntityFrameworkCodeFirstTriggers

The NuGet is at... https://www.nuget.org/packages/EntityFrameworkCodeFirstTriggers/ NuGet在... https://www.nuget.org/packages/EntityFrameworkCodeFirstTriggers/

You can put in logic which is executed when the entities are persisted (at SaveChanges() ). 您可以输入实体持久化时执行的逻辑(在SaveChanges() )。

customer.Inserting += (sender, args) => ((Customer)customer).Code = "Some default";

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

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