正如主题中简短描述的那样:如何在更新edmx时将所有DataAnnotations从模型移至MetaData模型,以免将其抹掉?
换句话说,我想确保数据注释的安全性,并且不希望每次更新edmx都会被删除,而我在dataannotation中将有一个选项来检查是否满足所有数据注释要求(IsValid方法)以在RelayCommand的CanExecute方法中使用它
我的课程如下:
public partial class Customer : IDataErrorInfo
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public int ID{ get; set; }
[Required(ErrorMessage = "Field required")]
public string Name{ get; set; }
[Required(ErrorMessage = "Field required")]
public string LastName{ get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblKontrahent> tblKontrahent { get; set; }
#region Validation
public bool IsValid { get; set; }
public string Error { get { return null; } }
public string this[string columnName]
{
get
{
Validation();
return InputValidation<Customer >.Validate(this, columnName);
}
}
public ICollection<string> AllErrors()
{
return InputValidation<Customer >.Validate(this);
}
private void Validation()
{
ICollection<string> allErrors = AllErrors();
if (allErrors.Count == 0)
IsValid = true;
else
IsValid = false;
}
#endregion
#region Shallow copy
public Customer ShallowCopy()
{
return (Customer )this.MemberwiseClone();
}
#endregion
}
如何使用批注和IsValid函数将其从模型移动到MetaDataModel。如果还可以移动ShallowCopy方法,那就太好了。
非常感谢您提出任何建议!。
答案 0 :(得分:0)
对于大多数重要的应用程序,我将EF类完全分开。我将属性从实体框架复制到可自我跟踪的视图模型。
对于较小的应用程序,我过去避免这样做。
您可以看到在此使用的方法:
https://gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204
使用INotifyDataErrorInfo,您将在BaseEntity中找到IsValid。这是一个非常复杂的类,但是可以重用。
您可能可以将浅表副本重构为BaseEntity。如果您可以在任何地方使用它,都可以轻松进行投射。
注释在单独的伙伴类中。您可以在Customer.metadata.cs和Product.metadata.cs中看到示例。这些是局部类,可将对BaseEntity的继承添加到实体类。因此,EF类Customer继承了BaseEntity。
一个例子:
using DataAnnotationsExtensions;
namespace wpf_EntityFramework.EntityData
{
[MetadataTypeAttribute(typeof(Product.ProductMetadata))]
public partial class Product : BaseEntity, IEntityWithId
{
public void MetaSetUp()
{
// In wpf you need to explicitly state the metadata file.
// Maybe this will be improved in future versions of EF.
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Product),
typeof(ProductMetadata)),
typeof(Product));
}
internal sealed class ProductMetadata
{
// Some of these datannotations rely on dataAnnotationsExtensions ( Nuget package )
[Required(ErrorMessage="Product Short Name is required")]
public string ProductShortName { get; set; }
[Required(ErrorMessage = "Product Weight is required")]
[Min(0.01, ErrorMessage = "Minimum weight is 0.01")]
[Max(70.00, ErrorMessage = "We don't sell anything weighing more than 70Kg")]
public Nullable<decimal> Weight { get; set; }
[Required(ErrorMessage = "Bar Code is required")]
[RegularExpression(@"[0-9]{11}$", ErrorMessage="Bar codes must be 11 digits")]
public string BarCode { get; set; }
[Required(ErrorMessage = "Price per product is required")]
[Range(0,200, ErrorMessage="Price must be 0 - £200") ]
public Nullable<decimal> PricePer { get; set; }
private ProductMetadata()
{ }
}
}
}
如评论中所述。
您需要在每个实例上调用该Metasetup。除非最近几年有所变化。伙伴类不只是像MVC一样。
该示例还从UI反馈转换失败。
请参阅Dictionary1中的模板。
<ControlTemplate x:Key="EditPopUp" TargetType="ContentControl">
<ControlTemplate.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ErrorToolTip}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
</ControlTemplate.Resources>
<Grid Visibility="{Binding IsInEditMode, Converter={StaticResource BooleanToVisibilityConverter}}"
Width="{Binding ElementName=dg, Path=ActualWidth}"
Height="{Binding ElementName=dg, Path=ActualHeight}"
>
<i:Interaction.Triggers>
<local:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
<e2c:EventToCommand
Command="{Binding EditVM.TheEntity.ConversionErrorCommand, Mode=OneWay}"
EventArgsConverter="{StaticResource BindingErrorEventArgsConverter}"
PassEventArgsToCommand="True" />
</local:RoutedEventTrigger>
<local:RoutedEventTrigger RoutedEvent="{x:Static Binding.SourceUpdatedEvent}">
<e2c:EventToCommand
Command="{Binding EditVM.TheEntity.SourceUpdatedCommand, Mode=OneWay}"
EventArgsConverter="{StaticResource BindingSourcePropertyConverter}"
PassEventArgsToCommand="True" />
</local:RoutedEventTrigger>
</i:Interaction.Triggers>