c# - Entity Framework 表约束

标签 c# entity-framework

我想把这样的对象存入数据库

public class Account
{
    [Key]
    public int Id { get; set; }

    [Index(IsUnique = true), Required]
    public string UserName { get; set; }

    [Required]
    public string Password { get; set; }

    [Required]
    public string Email { get; set; }
}

有什么方法可以告诉数据库属性 Email 必须有适当的形式吗?这样如果我想添加帐户

var account = new Account
{
     UserName = "X",
     Password = "X",
     Email = "NotValidEmail"
}

数据库不会接受该对象。

最佳答案

在 Email 属性上添加这个,

 [EmailAddress(ErrorMessage = "Invalid Mail Address")]

希望对你有帮助,

关于c# - Entity Framework 表约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120865/

相关文章:

c# - 按类型从表达式中提取所有条件

entity-framework - 为什么 EF5 代码在将可为空的日期时间插入数据库时​​首先使用 datetime2?

unit-testing - 如何在 Entity Framework 中使用 "Pex and Moles"库?

c# - .net Core EF - 按字符串名称或类型动态添加到 DbSet

c# - 将 Arduino 校验和函数转换为 C#

c# - XAML 到 HTML 的转换 - WPF RichTextBox

c# - 连接一个二维数组

c# - 如何使用 C# 从图像中裁剪十字矩形?

c# - Entity Framework : Invalid Column after removing the column

c# - EntityFramework 事务 - 保存到多个表