标签: data-annotations

网站地址验证的正则表达式

我有一个用户输入来提供网站地址,显然大多数用户不知道什么是格式良好的网址,因此我寻找一个遵循以下规则的网站地址正则表达式:

1) www.someaddress.com -正确
2) someaddress.com -正确
3) http://someaddress.com -正确
4) https://someaddress.com -正确
5) https://www.someaddress.co。 il -正确
6) http://www.someaddress.com -正确

我使用这个正则表达式

[RegularExpression(@"^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$", ErrorMessage = "Not a valid website address")]
public string SiteUrl { get; set; }
Run Code Online (Sandbox Code Playgroud)

但这是没有用的,因为它几乎允许所有字符串通过。

请提供数据注释答案,而不是诸如以下的答案:

Uri.IsWellFormedUriString
Run Code Online (Sandbox Code Playgroud)

因为.net 不支持自定义属性的客户端验证。

.net c# regex data-annotations

4
推荐指数
1
解决办法
681
查看次数

MetadataType问题

我正在使用VS2008 SP1,WCF Ria Service 2009年7月CTP.我发现MetadataType在部分类模式下不起作用,真的不知道我错过了什么:

工作:-

public partial class Person
{
    private string _Name;

    [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
    [StringLength(3)]
    public string Name
    {
        set{_Name = value;}
        get{return _Name;}
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person { Name="123432" };
        List res = new List();
        Validator.TryValidateObject(p,new ValidationContext(p,null,null),
            res,true);
        if (res.Count > 0)
        {
            Console.WriteLine(res[0].ErrorMessage);
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不行

public partial class Person
{
    private string _Name;

    public string Name
    {
        set{_Name = value;} …
Run Code Online (Sandbox Code Playgroud)

.net data-annotations

3
推荐指数
1
解决办法
6737
查看次数

实体框架4.1中具有不同名称的唯一列和导航属性的Fluent Mappings/Data Annotations?

首先,有没有办法通过使用数据注释或流畅的API告诉EF 4.1列需要是唯一的?

其次,关于导航属性,我有2个类Document和User.在文档类中,我有OwnerId(UserId)的属性和Owner(User)的属性.如何告诉EF 4.1 OwnerId是否真的是UserId而Owner是导航属性回用户?

文件类:

public abstract class Document: BaseEntity
    {
        public bool IsActive { get; set; }
        public string Description { get; set; }
        //The UserId
        public Guid OwnerId { get; set; }
        //The User
        public User Owner { get; set; }

    }
Run Code Online (Sandbox Code Playgroud)

mapping fluent-interface data-annotations entity-framework-4.1

3
推荐指数
1
解决办法
2619
查看次数

带有DataAnnotations的MVC模型验证 - 是否需要进行ICollection的任何方法?

我在Model类中有一个属性,如:

    /// <summary>
    /// A list of line items in the receipt
    /// </summary>      
    public ICollection<ReceiptItem> Items { get; set; }
Run Code Online (Sandbox Code Playgroud)

有没有什么办法可以标记这个属性来验证集合必须有1个或更多成员?我试图避免在ModelState.IsValid之外进行手动验证函数调用

data-annotations asp.net-mvc-3

3
推荐指数
1
解决办法
2724
查看次数

MetadataType和DataAnnotations无法正常工作

我已经盯着这个很长一段时间了,看不出问题.

我使用EF4和POCO生成器生成了一些POCO类.

我正在尝试使用MetadataType属性来创建我可以在我的View(asp.net MVC)中使用的"伙伴"类,并利用数据注释.

所以,我有一个名为Contact的域实体(POCO).这是在命名空间BreakAwayEntities中名为BreakAwayEntities的项目中.

我在View的项目(WebUI)中创建了一个公共部分类.就是这个:

namespace BreakAwayEntities
{
    [MetadataType(typeof(ContactMetadataSource))]
    public partial class Contact
    {
        internal sealed class ContactMetadataSource
        {
            [HiddenInput(DisplayValue = false)]
            public int ContactID { get; set; }

            [Display(Name = "First Name")]
            public string FirstName { get; set; }

            [Display(Name = "Last Name")]
            public string LastName { get; set; }

            public string Title { get; set; }
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我被处理以下错误消息:

错误10无法将类型'System.Collections.Generic.List [c:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll]'隐式转换为'System.Collections.Generic.列表[c:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll]'F:\ Code\Sandpit\GetAwayBreaks\WebUI\Controllers\CustomerController.cs 29 38 WebUI

由于某种原因,接线不起作用.部分类的名称空间是相同的,但它们位于不同的项目中.那应该没关系?在View中使用的Contact类不会显示单个属性.它应该暴露POCO的所有属性,由MetadataType增强.

如果我错过了一个集会,我没有得到任何关于它可能是什么的暗示.

我错过了什么?我需要在Global.asax中注册一些东西吗?谢谢

asp.net-mvc poco metadatatype data-annotations

3
推荐指数
1
解决办法
2370
查看次数

实体框架:如何使用Fluent API实现共享主键?

我有两个表,并希望使用其中一个PK作为PK.

这是我使用数据注释的实现:

public class User
{
    public System.Guid UserId { get; set; }
    public string UserName { get; set; }
}

public class Student
{
    [Key, ForeignKey("User")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public System.Guid StudentId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    // shared primary key 
    public virtual User User { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这里Student使用表User的主键.

如何使用Fluent API实现此功能?

(作为第二个问题,如果我从Student表中删除一个值,会产生级联删除吗?)

asp.net asp.net-mvc entity-framework data-annotations

3
推荐指数
1
解决办法
2051
查看次数

Powershell二进制模块 - 参数验证

我正在学习PowerShell,一位好朋友向我推荐了这篇关于PowerShell中二进制模块的精彩文章

我打算将我创建的一些控制台应用程序迁移到二进制模块中.

我想做的其中一件事就是正确验证我的PowerShell参数.

我发现这篇文章:

http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/

但是我不确定这是否是我应该遵循的方法,我的意思是,我甚至打算使用DataAnnotations在我的二进制模块中验证我的PowerShell参数

在不久的将来,我计划直接开始使用更多的PowerShell,但与此同时,我只想将我的控制台应用程序转换为PowerShell二进制模块.

你们能指出我正确的方向吗?

我只是想知道在二进制模块中是否有更多的PowerShell方式来处理我的参数

c# validation powershell data-annotations

3
推荐指数
1
解决办法
515
查看次数

有可能不使用数据注释属性ServiceStack OrmLite吗?

我正在尝试探索ServiceStack.OrmLite的功能,并且无法理解是否可以使用bootstrap类进行配置(外键,数据类型,列索引,别名等)?我不想在我的实体类上使用数据注释属性.即使使用某种配置也会比属性更好.那是因为我希望将来有机会更换ORM.也许存在用于流畅配置的第三方库?

data-annotations ormlite-servicestack

3
推荐指数
1
解决办法
2159
查看次数

使用数据注释的WPF MVVM验证

我是wpf mvvm架构的新手.插件更新正在使用linq到sql.I需要使用数据注释进行验证.我们如何使用数据注释提供验证.任何人都可以给出一些好的例子吗?

我的模型是

namespace EmployeeApp.Model
{
    public class Employee : ObservableObject
    {
        #region Constructor
        public Employee(int empId = 0, string empName = "", int age = 0)
        {
            _empId = empId;
            _empName = empName;
            _age = age;

        }
        #endregion

        #region Properties
        private int _empId = 0;

        public int EmpId
        {
            get { return _empId; }
            set
            {
                _empId = value;
                RaisePropertyChangedEvent("ID");
            }
        }

        private string _empName = string.Empty;

        public string EmpName
        {
            get { return _empName; }
            set
            {
                _empName …
Run Code Online (Sandbox Code Playgroud)

c# linq wpf mvvm data-annotations

3
推荐指数
1
解决办法
9770
查看次数

实体框架核心,DELETE CASCADE和[Required]

我在Entity Framework Core中遇到了一个DELETE CASCADE问题,我似乎找不到很好的解决方案。

这是我的模型的超级简化版:

User {UserID, Name}
Recipe {RecipeID, UserID}
Ingredient {IngredientID, UserID}
RecipeIngredient {RecipeID, IngredientID} *RecipeIngredient is the many-to-many table.
Run Code Online (Sandbox Code Playgroud)

Recipe和Ingredient都将UserID标记为[Required],并且RecipeIngredient将RecipeID和IngredientID标记为[Required]。

问题是SQL将不会创建数据库,因为存在多个到RecipeIngredient的级联删除路径(“引入FOREIGN KEY约束...可能会导致循环或多个级联路径”)。

所以我被困住了……我已经解决了一些想法,但是没有任何事情奏效。

  1. 这里有设计解决方案吗?我想保留我的外键,因为强制执行它是有意义的,但是如果有设计解决方案,我会接受的。

  2. 我的下一个想法是删除所有指向用户的FK-我必须在DELETE期间通过C#代码强制执行参照完整性,并且可以在CREATE期间使用[Required]强制输入数据。出现的问题-[必需]创建一个FK,并添加“ ON DELETE CASCADE”,这使我们重新回到多级联删除路径问题中。我真的很想保留[Required],因为它与Razor页面的集成巧妙,客户端验证和错误等。

  3. 下一个想法,在OnModelCreating(...)中将级联行为设置为SetNull:

    modelBuilder.Entity()。HasOne(i => i.User).WithMany(u => u.Ingredients).OnDelete(DeleteBehavior.SetNull);

    modelBuilder.Entity().HasOne(r => r.Source).WithMany(s => s.Recipes).OnDelete(DeleteBehavior.SetNull);

但这引发了一个异常,因为即使我在“成分和配方”中的属性设置为Nullable:

[Required(ErrorMessage = "User ID is required.")] 
public Nullable<int> UserID { get; set; }
Run Code Online (Sandbox Code Playgroud)

...由于[Required]属性,EF仍将其创建为NOT NULL数据库列。

有什么解决方案?据我所知,我应该将所有FK删除给User,然后尝试将其强制作为CREATE上的必填字段,但是我看不到要使用数据注释来做到这一点的方法。将此逻辑保留在我的代码优先模型中。

c# asp.net-mvc data-annotations entity-framework-core

3
推荐指数
1
解决办法
2632
查看次数