将数据注释添加到实体框架(或 Linq to SQL)生成的类

发布于 2024-12-13 15:11:10 字数 2489 浏览 2 评论 0原文

是否可以向 Entity Framework 添加更多 Data Anootation 成员,例如 RangeRequired... Linq to SQL 自动生成类?

我想在我的课程中使用数据注释验证,

谢谢

重要:与此主题相关:使用元数据和实体框架来使用数据注释进行验证

编辑1)

我为 Northwind 数据库创建了一个实体框架模型,并添加 Product 类。部分代码如下:

[EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
    #region Factory Method

    /// <summary>
    /// Create a new Product object.
    /// </summary>
    /// <param name="productID">Initial value of the ProductID property.</param>
    /// <param name="productName">Initial value of the ProductName property.</param>
    /// <param name="discontinued">Initial value of the Discontinued property.</param>
    public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
    {
        Product product = new Product();
        product.ProductID = productID;
        product.ProductName = productName;
        product.Discontinued = discontinued;
        return product;
    }

    #endregion
    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 ProductID
    {
        get
        {
            return _ProductID;
        }
        set
        {
            if (_ProductID != value)
            {
                OnProductIDChanging(value);
                ReportPropertyChanging("ProductID");
                _ProductID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("ProductID");
                OnProductIDChanged();
            }
        }
    }
    private global::System.Int32 _ProductID;
    partial void OnProductIDChanging(global::System.Int32 value);
    partial void OnProductIDChanged();

我希望 ProductID 是必需的,但我不能这样编写代码:

public partial class Product 
    {
        [Required(ErrorMessage="nima")]
        public global::System.Int32 ProductID;

    }

Is it possible to add more Data Anootation member such as Range,Required,... to Entity Framework or Linq to SQL generated classes automatically ?

I want to use data annotation validation for my classes

thanks

Important:Related to this topic: using Metadata with Entity Framework to validate using Data Annotation

EDIT 1)

I create an entity framework model for Northwind database and add Product class.a part of code is like this:

[EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
    #region Factory Method

    /// <summary>
    /// Create a new Product object.
    /// </summary>
    /// <param name="productID">Initial value of the ProductID property.</param>
    /// <param name="productName">Initial value of the ProductName property.</param>
    /// <param name="discontinued">Initial value of the Discontinued property.</param>
    public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
    {
        Product product = new Product();
        product.ProductID = productID;
        product.ProductName = productName;
        product.Discontinued = discontinued;
        return product;
    }

    #endregion
    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 ProductID
    {
        get
        {
            return _ProductID;
        }
        set
        {
            if (_ProductID != value)
            {
                OnProductIDChanging(value);
                ReportPropertyChanging("ProductID");
                _ProductID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("ProductID");
                OnProductIDChanged();
            }
        }
    }
    private global::System.Int32 _ProductID;
    partial void OnProductIDChanging(global::System.Int32 value);
    partial void OnProductIDChanged();

I want ProductID being Required but I can't write code this way:

public partial class Product 
    {
        [Required(ErrorMessage="nima")]
        public global::System.Int32 ProductID;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2024-12-20 15:11:10

是的。您需要为每个实体创建第二个部分类,并将其链接到具有替代属性的辅助类。

假设您有一个生成的部分类 Customer { public string Name { get;放; } }
生成的类将始终被标记为部分类。

然后您需要添加一个文件:

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
    // it's possible to add logic and non-mapped properties here
}


public class CustomerMetadata
{
    [Required(ErrorMessage="Name is required")] 
    public object Name { get; set; }   // note the 'object' type, can be anything
}

就我个人而言,我不认为这是一个非常优雅的解决方案,但它确实有效。

Yes. You need to create a 2nd partial class for each entity and link it to an auxiliary class with substitute properties.

Suppose you have a generated partial class Customer { public string Name { get; set; } }
The generated class will always be marked as partial.

Then you need to add a file with :

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
    // it's possible to add logic and non-mapped properties here
}


public class CustomerMetadata
{
    [Required(ErrorMessage="Name is required")] 
    public object Name { get; set; }   // note the 'object' type, can be anything
}

Personally I don't consider this a very elegant solution but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文