Csharp/C#教程:entity framework代码第一个CTP4默认列值?分享


entity framework代码第一个CTP4默认列值?

我一直在寻找使用Entity Framework CTP4的Code First,您可以使用ModelBuilder来构建表列。 有没有办法使用ModelBuilder或其他一些机制为数据库中的列设置默认值?

谢谢!

无法通过文本编辑器/应用程序找到添加默认值而非手动编辑的方法这是entity framework中的错误…

我正在使用构造函数来设置默认值。 从未失败过我

public class Activity { [Required] public DateTime AddedDate { get; set; } public Activity() { AddedDate = DateTime.Now; } } 

在为迁移生成的代码中,您可以为列指定默认值:

 AddColumn("users", "ReceiveSummaryEmail", c => c.Boolean(nullable: false, defaultValue: true)); 

这是我的方式,通过构造函数在创建方法中使用private setters设置重要属性

模型

 public class User { public static User Create(Action init) { var user = new User(); user.Guid = Guid.NewGuid(); user.Since = DateTime.Now; init(user); return user; } public int UserID { get; set; } public virtual ICollection Roles { get; set; } public virtual ICollection Widgets { get; set; } [StringLength(50), Required] public string Name { get; set; } [EmailAddress, Required] public string Email { get; set; } [StringLength(255), Required] public string Password { get; set; } [StringLength(16), Required] public string Salt { get; set; } public DateTime Since { get; private set; } public Guid Guid { get; private set; } } 

创建新用户

上述就是C#学习教程:entity framework代码第一个CTP4默认列值?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 context.Users.Add(User.Create(c=> { c.Name = "User"; c.Email = "some@one.com"; c.Salt = salt; c.Password = "mypass"; c.Roles = new List { adminRole, userRole }; })); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1002167.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐